Thread: class operator +

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    41

    Unhappy class operator +

    Hi!

    I'm trying to learn operator overloading. But I have some problems grasping the basic idea.

    Let's say we have this class:

    Code:
    class tutClass
    {
      char * data;
    
    public:
      tutClass() : data(NULL) {};
      tutClass(char *);
      ~tutClass();
    
      conversion operator + (char *);
      conversion operator + (tutClass &);
    }
    
    tutClass::tutClass(char * str)
    {
      data = new char [ strlen(str) + 1 ];
      strcpy(data, str);
    }
    
    ~tutClass()
    {
      if (data)
        delete [] data;
    }
    
    tutClass tutClass::operator + (char * str)
    {
      //??? what goes here?
      //??? something like this?
    
      char * temp = new char [ strlen(str) + strlen(data) + 1 ];
      strcpy(temp, data);
      strcat(temp, str);
    
      tutClass result(str);
    
      delete [] temp;
    
      return result; //this isn't right, returning a local var
    //but how to do it right?
    }
    //same for tutClass::operator + (tutClass & tut)
    I probably should implement operator = also to make this work, but this is just to show "+"...

    And should it return tutClass, or tutClass &

    Any help would be appreciated!

    Joren

  2. #2
    Registered User
    Join Date
    Aug 2001
    Posts
    223
    Check out how microsoft does it....
    where string1 is the left and string2 is the right
    then a new object (s) is created and returned...found it the file

    C:\Program Files\Microsoft Visual Studio\VC98\MFC\SRC\strcore.cpp


    Code:
    CString AFXAPI operator+(const CString& string1, const CString& string2)
    {
    	CString s;
    
    
    	s.ConcatCopy(string1.GetData()->nDataLength, string1.m_pchData,
    		string2.GetData()->nDataLength, string2.m_pchData);
    
    
    	return s;
    }
    
    void CString::ConcatCopy(int nSrc1Len, LPCTSTR lpszSrc1Data,
    	int nSrc2Len, LPCTSTR lpszSrc2Data)
    {
      // -- master concatenation routine
      // Concatenate two sources
      // -- assume that 'this' is a new CString object
    
    	int nNewLen = nSrc1Len + nSrc2Len;
    	if (nNewLen != 0)
    	{
    		AllocBuffer(nNewLen);
    		memcpy(m_pchData, lpszSrc1Data, nSrc1Len*sizeof(TCHAR));
    		memcpy(m_pchData+nSrc1Len, lpszSrc2Data, nSrc2Len*sizeof(TCHAR));
    	}
    }
    zMan

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    41
    But isn't s a local variable? And you shouldn't return a local variable, because it goes out of scope on return s; If not, I don't understand anything anymore. At least not about scope

  4. #4
    S­énior Member
    Join Date
    Jan 2002
    Posts
    982
    >But isn't s a local variable?

    Yes, but when you're returning by value a copy is made. If the code was returing a pointer or reference to a local object created on the stack you'd be correct.

  5. #5
    Unregistered
    Guest
    try to think of operator overloading as a way of relating 2 of the same objects with operators eg =, +. / etc, etc. for example with 3d vectors my + operator is like this:

    Code:
    Vector3d operator +( const Vector3d& a, const Vector3d& b )
    {
    	Vector3d temp;
    	int i;
    
    	for( i = 0; i < 3; i++ )
    	{
    		temp.triple[ i ] = b.triple[ i ] + a.triple[ i ];
    	}
    
    	return temp;
    }
    the vector is like an x, y, z coordinate and this is the way to add 2 togethor - dead simple.

    Lets try another with the following class....

    Code:
    class Date
    {
    public:
          Date( );
          friend ostream& operator << ( ostream& os, const Date );
    
    private:
         int day;
         int month;
         int year;
    }
    
    //implementation of <<
    ostream& operator << ( ostream& os, const Date )
    {
          cout << day << " : " << month << " : " << year;
    }
    this is a simple example of insertion overloading but I hope you get the idea

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 7
    Last Post: 11-10-2007, 05:17 AM
  2. Destructor - Execution problem
    By triste in forum C++ Programming
    Replies: 16
    Last Post: 09-26-2004, 01:57 PM
  3. Class assignment operator
    By Unregistered in forum C++ Programming
    Replies: 7
    Last Post: 03-02-2002, 10:37 PM
  4. overloading a new operator in a Class
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 02-23-2002, 02:47 PM
  5. Difficulty superclassing EDIT window class
    By cDir in forum Windows Programming
    Replies: 7
    Last Post: 02-21-2002, 05:06 PM