Function creates memory space and returns reference to class
What I want to do is have a function create the memory space outside of the stack for a class and then return the object via a reference. Is there any problem using the below method?
Code:
#include <iostream>
class test {
public:
test *ptr;
int x;
test(test *x){ ptr = x; }
~test() { delete ptr; }
};
test & func1(void);
int main(void)
{
test &x = func1();
std::cout<<x.x<<std::endl;
std::cout<<x.ptr<<std::endl;
}
test & func1(void)
{
test *x = new test(x);
x->x = 12;
return *x;
}
It compiles and runs fine. Just wondering if there is any potential problems I should be aware of. Thanks