I have some questions on returning a reference to a local variable from a function:
class A {
public:
A(int xx)
: x(xx)
{
printf(\"A::A()
If you will compile this on VC6 you will get this warning
******Compiler Warning (level 1) C4172 returning address of local variable or temporary A function returns the address of a local variable or temporary object. Local variables and temporary objects are destroyed when a function returns, so the address returned is not valid.******
While testing for this problem i found interesting thing (given code is working in VC6):
class MyClass
{
public:
MyClass()
{
objID=++cntr;
}
MyClass& myFunc()
{
MyClass obj;
return obj;
}
int objID;
static int cntr;
};
int MyClass::cntr;
main()
{
MyClass tseadf;
cout<<(tseadf.myFunc()).objID<