W
Wang Tong
Suppose I have a function foo() and a class definition myClass, and I
want to create an instance of myClass and return it from foo(). I guess
the most efficient way is to return a reference to the object.
But here comes the problem: the object is a local one and will be
detroyed after function return. How can I return the reference from foo()?
Code:
myClass& foo()
{
myClass myObject;
...
return myObject; //Oops, myObject is a local one
}
want to create an instance of myClass and return it from foo(). I guess
the most efficient way is to return a reference to the object.
But here comes the problem: the object is a local one and will be
detroyed after function return. How can I return the reference from foo()?
Code:
myClass& foo()
{
myClass myObject;
...
return myObject; //Oops, myObject is a local one
}