CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    38

    Resolved Custom operator overloading question (Resolved)

    Hello everybody

    This is kind of a newbie question but I didn`t find a specific answer in the FAQ. When I overload the addition operator I always create a new unnamed object which is returned to the calling statement. Something like in this code snippet :

    Code:
    AClass AClass::operator + (AClass A1)
    {
      int sum;
    
      sum = internalValue + A1.internalValue;
      return AClass(sum);
    }
    which means that I create a new temporary object. In the main code I can thus use something like :

    Code:
      AClass a1(10), a2(10), a3;
      
      a3 = a1 + a2;
    Now which object does a3 point to? The one created by it`s definition or to the one returned by the overloaded operator?
    Last edited by divined; February 5th, 2006 at 06:09 AM.

  2. #2
    Join Date
    Oct 2005
    Location
    England
    Posts
    803

    Re: Custom operator overloading question

    It points to the object created by its definition, the tempory class is just used to assgin the new value to that object.

    Rich

  3. #3
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Custom operator overloading question

    >>Now which object does a3 point to? The one created by it`s definition or to the one returned by the overloaded operator?
    a3 is still the same object. The compiler calls the assignement operator of a3 to assign it the temporary object returned from operator +.
    The assignement operator is one of the functions the compiler automatically creates if you don't provide one.
    Kurt
    EDIT: too slow
    Last edited by ZuK; February 5th, 2006 at 04:51 AM.

  4. #4
    Join Date
    Feb 2005
    Location
    "The Capital"
    Posts
    5,306

    Re: Custom operator overloading question

    Your overload for the operator+ is not a very good one! You need to overload the operator+= as well and when you do that it's better to overload operator+ in terms of operator+=. Also, when you pass the arguments by pass by value - there is a good alternative to that - passing const reference of the object. Also, sometimes just the member overloads don't suffice all the needs.. you need to provide friend overloads as well for full support to your classes. See this thread for details - Operator overloading - operator+/operator+=. Hope this helps. Regards.
    Last edited by exterminator; February 5th, 2006 at 06:15 AM. Reason: grrrr typose...

  5. #5
    Join Date
    Apr 2005
    Posts
    38

    Re: Custom operator overloading question

    thanks guys! I got the general idea. I`d practically forgotten about the assignment operator and thought there would be two objects left, thus wasting memory space.

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