My question is simply, will this lead to memory leaks with any otherwise perfect class) or does the compiler know when the newly created variable (created with new but returned as a reference (not a pointer)) will go out of scope? Thanks a lot - Joakim.
Code:
#include <iostream>
class Foo
{
public:
Foo(bool blah) { foo = blah; std::cout << "init" << foo << std::endl; }
~Foo() { std::cout << "exit" << foo << std::endl; }
bool foo;
};
Foo& test() { return *new Foo(false); }
int main()
{
Foo a(true);
a = test();
}
/*
output: (1 is for a true value and 0 is for a false value)
init1
init0
exit0
*/