Hello everyone,
I am confused what is the type of *this? Type of reference to instance or type of instance?
Looks like both code can compile, no warning messages. Any ideas?
-
class A{
-
-
A& foo1()
-
{
-
return *this;
-
}
-
-
A foo2()
-
{
-
return *this;
-
}
-
};
-
thanks in advance,
George
As far as I think, when you are thrying the first code, i.e.,
- A& foo1()
-
{
-
return *this;
-
}
-
the original object is returned (with updatations if assigned), but for the second case, i.e.,
- A foo1()
-
{
-
return *this;
-
}
-
a copy of the original object is returned. The compiler will not complain because return by value is acceptable, but it will undoubtedly waste some memory space.
I haven't tried this at home, but it is true that is any other case if you try to return by address in the function body and do otherwise in function declaration, the compiler gives you an error!
Let's see what other people have to say on this....
Sagnik