Valhalla Legends Forums Archive | C/C++ Programming | this pointer and returning objects

AuthorMessageTime
DVX
i've been confused about this for awhile.. but what exactly is returned when you return an object? additionally, what exactly does the this pointer return when it's deferenced? it's just confused me..
February 13, 2004, 1:33 AM
iago
It generally points to an instance of the object in memory. The non-static fields, that is. Because object tend to be big (do a sizeof() on your favourite object), passing or returning the actual parameter is generally a bad idea. To save both you and your computer effort, do what Java does and just always use pointers for objects :)
February 13, 2004, 2:21 AM
DVX
[code]
SomeClass function()
{
SomeClass SomeObject;
otherstatements;
return SomeObject;
}
[/code]

what does the return SomeObject statememnt actually return... that's what i get confused about.. what value gets returned?
February 13, 2004, 2:27 AM
Kp
[quote author=DVX link=board=30;threadid=5260;start=0#msg43965 date=1076639260][code]SomeClass function()
{
SomeClass SomeObject;
otherstatements;
return SomeObject;
}[/code]what does the return SomeObject statememnt actually return... that's what i get confused about.. what value gets returned?[/quote]

Through methods that are somewhat compiler specific in the gory details, it returns an instance of SomeClass, with the fields set to be the same as those of SomeObject. Some compilers implement this by performing a shallow copy immediately upon return from the function; others implement it by adding a SomeClass* to the parameter list and writing to the pointed-at value (or copying to it) just before returning. As iago said, passing and returning objects is generally a bad idea because of the excess copying. Pass pointers instead. Do not emulate Java if avoidable.
February 13, 2004, 2:30 AM
iago
[quote author=Kp link=board=30;threadid=5260;start=0#msg43967 date=1076639459]
Pass pointers instead. Do not emulate Java if avoidable.
[/quote]

But.. that's what Java does :P
February 13, 2004, 2:50 AM
DVX
i've seen declarations like this: SomeClass SomeObjectA = SomeObjectB;

what does this mean...? what exactly is the "value" of SomeObjectB..?
February 13, 2004, 3:34 AM
Kp
[quote author=DVX link=board=30;threadid=5260;start=0#msg43977 date=1076643294]
i've seen declarations like this: SomeClass SomeObjectA = SomeObjectB;
what does this mean...? what exactly is the "value" of SomeObjectB..?[/quote]

Whatever it contains. If the programmer hasn't overriden assignment, that statement will perform a shallow copy of ObjectB to ObjectA. Shallow copies are fine, provided that the object has no pointers within it. If it does, both copies will point to the same original data (which could be bad if the code for SomeClass assumes that it has exclusive ownership of that data).
February 13, 2004, 3:41 AM

Search