CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Threaded View

  1. #1
    Join Date
    May 2009
    Posts
    5

    Unhappy operator overloading

    hello everyone!
    I met some problems with pointer here:
    Code:
    ////////////////////////code//////////////////////////////////
    //class's declaration 
    //file FunnyString.h
    #ifndef FUNNYSTRING_H
    #define FUNNYSTRING_H
    #include <cassert>
    #include <iostream>
    
    using namespace std;
    class FunnyString
    {
    	friend istream &operator>>( istream&, FunnyString& );
    	friend ostream &operator<<( ostream&, FunnyString&) ;
    public:
    	FunnyString(char* = 0);
    	~FunnyString()
    	{
    		delete [] data;
    	}
    	FunnyString( FunnyString& ); //copy constructor.
    	FunnyString  operator+( FunnyString& );
    	FunnyString & operator=(  FunnyString& ); //assigment operator.
    private:
    	void SetString( char*);
    	char GetOneCharacter( const int & index )
    	{
    		assert( ( index < len ) && index>= 0 );
    		return data[index];
    	}
    	void CutBeginCharacter();
    	void SelfCutEndCharacter();
    	char* data;
    	int len;
    };
    #endif
    ///////////////////////////code///////////////////////////////////////////////
    -----------------------------code--------------------------------------------
    //FunnyString class's definition.
    //FunnyString.cpp file.
    #include "FunnyString.h"
    #include <iostream>
    #include <cassert>
    using namespace std;
    
    FunnyString::FunnyString(char *s )
    :len(strlen(s)+1)
    {
    	SetString(s);
    }
    FunnyString::FunnyString( FunnyString & s)
    {
    	SetString(s.data);
    }
    FunnyString& FunnyString::operator= (  FunnyString& s)
    {
    	if ( &s != this ) //prevent self assigment.
    	{
    		if ( s.len != len )
    		{
    			delete [] data;
    			SetString(s.data);
    		}
    	}
    	else cout << "Self assigment\n";
    	return *this;
    }
    FunnyString  FunnyString::operator+( FunnyString & s)
    {
    	FunnyString fString1(data);
    	FunnyString fString2(s.data);
            FunnyString &tmp = fString1;
    	while ( fString1.data[len-2] == fString2.data[0] )
    	{
    		fString1.SelfCutEndCharacter();
    		fString2.CutBeginCharacter();
    	}
    	strcat(fString1.data,fString2.data);
    	return tmp;
    }
    ostream& operator<< (ostream& output,FunnyString& s )
    {
    	output << s.data;
    	return output;
    }
    istream& operator>> (istream& input, FunnyString& s)
    {
    	char tmp[100];
    	input >> tmp;
    	s.data = tmp;
    	return input;
    }
    void FunnyString::SetString( char* s )
    {
    	if ( 0 != s )
    	{
    		len = strlen(s)+1;
    		data = new char[len];
    		strcpy(data,s);
    	}
    	else 
    	{
    		len = 1;
    		data = 0;
    	}
    }
    void FunnyString::CutBeginCharacter()
    {
    	char* tmp = new char[len-1];
    	for ( int i = 0; i < len - 2; i++)
    	{
    		tmp[i]=data[i+1];
    	}
    	tmp[len-2] = 0;
    	delete [] data;
    	SetString(tmp);
    	delete [] tmp;
    }
    void FunnyString::SelfCutEndCharacter()
    {
    	char* tmp = new char[len-1];
    	strncpy(tmp,data,len-2);
    	tmp[len-2] = 0;
    	delete [] data;
    	SetString(tmp);
    	delete [] tmp;	//release tmp;
    }
    -----------------------------code-------------------------------------

    The operator+ function met a pointer problem(I don't know why, I just want this function does not change the data, so I created 2 new FunnyString objects and handle them. This function return a reference of FunnyString!!!- maybe when the function end, all the data will be destruct by destructor which released the char* data, so function can not return it).But I can't prevent it. this is a operator overloading, it can not contain more parameter,so how can I return in this function?
    Last edited by HanneSThEGreaT; May 13th, 2009 at 10:44 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured