Archived

This topic is now archived and is closed to further replies.

way_out_west

Returning Object Variables From Functions By Value

Recommended Posts

I''m trying to make sense of returning by value and in doing so, I set up a class to map every function call that could be called whilst returning objects by value. #include <iostream.h> class Animal { public: Animal() { Age = 0; cout << "Animal()\n"; } Animal(int itsAge): Age(itsAge) { cout << "Animal(int)\n"; } Animal(const Animal& rhs): Age(rhs.Age) { cout << "Animal(const Animal& ()\n"; } Animal operator=(const Animal& rhs) { Age = rhs.Age; cout << "operator=()"; return *this; } void DisplayAge() { cout << Age << "\n"; } private: int Age; }; Animal SomeFunction() { return Animal(4); } int main() { Animal John = SomeFunction(); return 0; } output: Animal(int) I''m having trouble understanding why this line: Animal John = SomeFunction(); does not call the copy constructor but instead appears to refer to the unnamed temporary object created and returned by Somefunction(). could someome please explain why this is so?

Share this post


Link to post
Share on other sites
The compiler has optimized your code. Instead of creating a temporary object and then creating your John object by using the copy-ctor, compiler is so 'smart' that it only creates your John object using the ordinary ctor and skipping the temporary object all together.

Note 1: Use <iostream> not <iostream.h>.
Note 2: operator= should return a reference. Animal& operator=(const Animal& rhs)



Update GameDev.net system time campaign: 'date ddmmHHMMYYYY'

[edited by - dalleboy on January 20, 2003 5:17:48 AM]

Share this post


Link to post
Share on other sites
Thanks dalleboy.

I've one question relating to your reply;

So you're saying that this specific compiler enforces an exception rather than a rule?

I've some idea how this works, but I don't want to get myself wrong. If it sounds like I'm answering my own question, that's definately not my intention. I just want to make sure I understand this topic- I find it quite challenging.



Is it right that, generally speaking, when the compiler hits line 3 (below):

1: Animal SomeFunction()
2: {
3: return Animal(4);
4: }

the general constructor is called to create and initialize this object, whose space has already been reserved before the function block is entered. Then, that area of stack memory is referred to as the actual value of the function call in client space?

Finally, The object John is created via the call to the copy constructor (that takes this area of unamed stack memory as the actual argument)

1: int main()
2: {
3: Animal John = SomeFunction();
4:
5: return 0;
6: }

Is all this correct? And... one last question.

When you create and object in a function and then return this object by value

//Function Definition
Animal SomeFunction()
{
Animal Temp(4);
return Temp;
}

//Client Code
{
SomeFunction();
}

Is it the Local object 'Temp' that gets destroyed first? Or is it the object that is copied on the stack after the return statement.

As you can see I'm having really hard time trying to comprehend this idea and I really do appreciate all your help.


[edited by - way_out_west on January 20, 2003 8:41:43 AM]

Share this post


Link to post
Share on other sites