I have seen return types as references (*this), and als o just regular types (thing temp; return temp). How are they diferent/when would you use each one?
Printable View
I have seen return types as references (*this), and als o just regular types (thing temp; return temp). How are they diferent/when would you use each one?
Actually, when someone returns a *this, it does not return a reference, because the this pointer as it's called, is already a pointer, so by saying *this, you dereference it, so you return the actual object, not just a pointer to it..
But for your actual question, I suggest that you read: Teach Yourself C++ in 21 Days,
Second Edition, which you can find here:
http://newdata.box.sk/bx/c/
The topics Pointers and References will answer your question I think...
Generally:
return temp;
means that temp is a value that is passed back directly to the calling function. IF temp is a char and its 'A' then 65 (Ascii A - assuming no unicode) is placed in a register and returned.
This is used when the called function creates the value. It's called passing by value.
return *temp;
means that you are working with an entity external to the function - you are working with it by reference. This is used only when the object or datatype referred to is external to the function or class. This is because once the function exits, any data not directly returned to the calling program is destroyed, so passing a pointer to nothing is not useful.
Jop - I don't believe he meant this in the strict C++ sense.
He wanted to know the difference between by value and by reference.
But you are correct about dereferencing.
Actually, what I was talking about (and forgot to mention) was operators for classes.
I'm sorry, but what is your actual question? operators? or return objects from classes?
He is talking about this:
Ok, let's see...Code:MyClass& MyClass::operator = (MyClass &other)
{
m_a = other.m_a;
// ...
return *this;
}
The normal return is a return by value. jim already explained that. If the returned object is not a value but rather a complete object, the whole object is copied (which is slow).
If you have a function that returns a large object, you might therefore want to return a reference to it to save time:
This does not work because the object at which the reference points is not valid anymore once the function returns. You either have to copy it, or pass a reference to such an object that is declared in the caller's scope.Code:LARGESTRUCT & myFunc()
{
LARGESTRUCT ls = {213, 32, 4567, 234235, 234.35, 0x2342F, ...};
return ls;
}
Now, with operators the arguments are clearly defined, so this doesn't work, you can't add the necessary argument and you want to have the return value. Like this:
MyClass o1, o2, o3;
o1 = o2 = o3 = someO;
You need it as return value. Luckily in class members you have the this pointer, and the class object is valid even after returning because it isn't a local object. Therefore you can use it to return by reference and you don't need to make a copy. But since this is a pointer, you need to dereference it. The reult is
return *this;
You would write that even if the function declaration would look like this (no reference):
MyClass operator =(MyClass &other);
You should take a look at the syntactical differences between pointers and references.