Hi all,
I have a major problem with my code when attempting to dereference a pointer contained within a structure that is ifself an instance member within an Interface object.
The following code snipit is a simplication of the issue at hand.
First suppose I have a struct, and in the definition of the struct I delcare a coupple of pointers to some class-types.
Next, I define an interface object which has one method and contains a pointer to a the struct as a member attribute.
Next, I define a class type C that conforms to the interface object I. During the constructor call - the member attribute pointer is allocated.And we can print out the memory address of the object A and object B
Next, in some general class an instance of C is created and added to a std::List container object - defined to hold Interface Object I
Finally, in some general class D we attempt to get back the interface Object and dereference it. This is where the issue lies. We use an itterator to get to the Interface object. Using this object we can attempt to get to the stuct (and this works - we can successfully print the value of member _id).
But, when attempting to get the ObjTypeA amd ObjTypeB the pointers have changed. They are no longer pointing to the instances.
Has anyone ever experienced anything like this before? What am I doing wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
|
struct MyStruct
{
int _id;
ObjTypeA *a
ObjTypeB *b;
};
class I
{
public:
virtual const MyStruct * getMyStruct() = 0;
MyStruct *_data;
};
class C : public I
{
class C()
{
// do stuff
_data = new MyStruct();
_data.a = new ObjTypeA();
_data.b = new ObjTypeB();
cout << "id " << hex << _data->_id << endl;
cout << "object A" << hex << _data->a << endl;
cout << "object B" << hex << _data->b << endl;
}
};
... some other user set-up class
{
...
C *anObj = new C();
// list is a std::list <I *>
D->addToList (C)
D->processList()
}
.... Class D
processList ()
{
// just grab first item for now
std::list <I *>::iterator it = mylist.begin();
I *obj = *(it);
cout << "id " << hex << obj->_data._id << endl;
// problem is here - the output values do not match
// expected values
cout << "object A" << hex << obj->_data.a << endl;
cout << "object B" << hex << obj->_data.b << endl;
}
| |