gg9h0st wrote:
i worte a simple code below.
Are you sure you wrote it and not some automatic code generator that
comes with your compiler?
------------------------------------------------------------------------------------
#include "stdafx.h"
What's stdafx.h?
class Object {
public:
int a;
Object() {
a = 100;
}
};
Side issues: Your data member should be private and your constructor
should use an intialiser list, not assignment in the constructor body.
Object aFunction() {
That signature says that aFunction returns an object of type "Object"
by value.
return Object();
}
This statement creates an unnamed temporary object of type "Object" and
returns it. The unnamed temporary ceases to exist at the end of this
statement. Because the function returns by value, before the unnamed
temporary is destroyed, a copy is made and that copy is returned to the
function that called aFunction.
Object& bFunction() {
That signature says that bFunction returns a reference to an object of
type "Object". A reference is not the object itself, it is an alias for
the object. So the object referred to needs to exist somewhere.
return Object();
This statement creates an unnamed temporary object of type "Object" and
returns it. The unnamed temporary ceases to exist at the end of this
statement. Because the function returns by reference, the returned
reference is intialised with (and so is an alias for) this unnamed
temporary. But as soon as this statement ends, the unnamed temporary is
destroyed. The reference is returned to the function that called
bFunction, but the object to which that reference refers no longer
exists. Any attept to use that reference invokes undefined behavior.
}
int _tmain(int argc, _TCHAR* argv[])
The _tmain and _TCHAR parts of that code are not standard C++
{
std::cout << aFunction().a << std::endl;
aFunction returns an object. That object is a copy of the unnamed
temporary that was created and destroyed within aFunction. So this code
will output 100.
std::cout << bFunction().a << std::endl;
bFunction returns a reference to an object. That object referred to is
the unnamed temporary that was created and destroyed within bFunction.
Because the referred to object has been destroyed, the behaviour of
your code is undefined. Anything can happen.
return 0;
}
----------------------------------------------------------------------------------
as i see the two different return type, Object, Object&, do same thing.
the results are both printing 100.
That's because you were unlucky. The use of the return value of
bFunction invokes undefined behaviour. Anything can happen and one of
the possbilities is that the code does exaclt what you think it should
do. As far as the C++ language is concerned, the behaviour would be no
more or less correct if it printed 200 instead of 100, or if it printed
"hello world", or if it emailed "hello world" to everyone in your
address book, or if it formatted your hard drive.
but i see a warnning message from compiling this code.
Good. Your compiler may well have a setting that lets you treat
warnings as errors and so prevent successful compiling if a warning is
issued. If you can find this setting, turn it on. The compiler is your
friend. Help it to help you.
what's the difference between Object and Object&?
In your case, the latter invokes undefined behaviour, which is a Bad
Thing.
i see Object& as a return type and an argument.
http://www.parashift.com/c++-faq-lite/references.html
Gavin Deane