-
February 5th, 2006, 04:30 AM
#1
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.
-
February 5th, 2006, 04:44 AM
#2
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
-
February 5th, 2006, 04:48 AM
#3
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.
-
February 5th, 2006, 05:41 AM
#4
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...
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
February 5th, 2006, 06:08 AM
#5
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|