1

I have the following problem. I create a class, and store the pointer to that class in an other class. Upon creating, everything is OK. However, a single step later it seems that the class has disappeared.

I've written a very simple test scenario here:

#include <iostream>

using namespace std;

class test
{
    public:
        test();
        bool ok;
};

test::test()
{
    ok = false;
}

class func
{
    public:
        func();
        void check();
        test *pTest;
};

func::func()
{
    test temptest = test();
    cout << temptest.ok << endl;
    pTest = &temptest;
    cout << pTest->ok << endl;
}

void func::check()
{
    cout << pTest->ok << endl;
};

int main( int argc, char *argv[] )
{
    func mFunc = func();
    // what happens here
    mFunc.check();
}

The above program outputs the following:

0
0
204

From 204 I'd guess that somehow the class I've created before has disappeared.

Can you tell me what is happening and why?

hyperknot
  • 13,454
  • 24
  • 98
  • 153

4 Answers4

1

All the above are correct. The simple solution that I expect you are seeking is

pTest = new test();

instead of assigning to the local variable and then taking a pointer to that.

GaryBishop
  • 3,204
  • 2
  • 23
  • 19
1

The problem is that you are creating a an object with a "limited" scope.

func::func()
{
    test temptest = test();             // temptest construction
    cout << temptest.ok << endl;
    pTest = &temptest;
    cout << pTest->ok << endl;
}                                       // temptest descrution 

After the construction of func, pTest now refer to an invalid object.

You have to use dynamic memory or shared pointers to manage pointers.

#include <iostream>

using namespace std;

class test
{
    public:
        test();
        bool ok;
};

test::test()
{
    ok = false;
}

class func
{
    public:
        func();
       ~func();
        void check();
        test *pTest;
};

func::func()
{
    pTest = new Test();
    cout << pTest->ok << endl;
    cout << pTest->ok << endl;
}
func::~func() { delete pTest; }

void func::check()
{
    cout << pTest->ok << endl;
};

int main( int argc, char *argv[] )
{
    func mFunc = func();
    // what happens here
    mFunc.check();
}

Now the constructor of test allocate a new object and store the address of that object, and the destructor can deallocate memory. Manage memory in this way is not a good prectice.

Use shared pointers instead like shared_ptr or unique_ptr, but this requires some other knowledge such move semantic.

Elvis Dukaj
  • 7,142
  • 12
  • 43
  • 85
0

The lifetime of the func object is beyond the lifetime of the contained pTest object instance.

Once the constructor func returns, the object pointed to by its member pTest is now no longer valid (temptest, an auto/stack instance of test within the scope of func::func() has been destroyed when func::func returns).

franji1
  • 3,088
  • 2
  • 23
  • 43
0

The object temptest is allocated on the stack when a call to func class function func() is made. The address of this memory is then stored in the pointer pTest. When function func() goes out of scope the stack unwinds and the object temptest is destructed. The pointer pTest now points to unallocated data which yields undefined results.

Objects created like this:

test t;

is called automatic objects and is automatically deallocated when they go out of scope.

You should read into the differences of dynamically allocating data and using automatic objects. You could also read about stack and heap memory, which I think can bring some light to the matter.

Felix Glas
  • 15,065
  • 7
  • 53
  • 82
  • The standard doesn't mention "heap" vs "stack", and they are irrelevant implementation details as far as the question is concerned. The magic words here are *storage duration* (particularly, automatic vs dynamic). – cHao Dec 08 '12 at 23:01