Click to See Complete Forum and Search --> : Pointer Object


Kohinoor24
June 19th, 2002, 07:06 AM
I Posted this post earlier,but I guess I didnt express really what I wanted.(So I was getting answers which was not helpful for me).

For Eg;
Iam Having a
Class A
{
inline CycleBuffer& GetCycleBuffer(){return *m_pCycleBuffer;}
Public:
DesignDocumentData* m_pDesignDocumentData;
LdMapDocumentData* m_pLdMapDocumentData;

MapDocumentData* m_pMapDocumentData;

NgDocData m_ngDocData;
I_NgDocDataRead* m_pNgDocData;
CyclicBuffer *m_pCyclicBuffer;


};

IN a class called say ,"C"
I have created the class
A *ptr = new A;
and I have assigned each variable with some value.

Now Iam in class B & I want to access some values which are there in class A;
So in
Class B
{
B(){ *ptr = NULL;}
GetDATA(/* Want to acces data using the A *ptr; */);

Private:

A *ptr;
};

So if I create a different object with "new" operator,I guess I wont be able to access the datas stored earlier & I dont want to make the members static in class A.
I want to have the same object instance which was created in class C.

How can I do it?could anyone show it with a code snippet.

Thanks in advance

Elrond
June 19th, 2002, 09:14 AM
Simple in the case you know your instance of C where you create your instance of B. Then you just have to do something like:

B.ptr = C.ptr;

If you don't have a way to know C where B is created, then you can create a static variable in the class A that contains the list of all instances of A:


class A
{
...
public:
static vector<A*> arrA;
...
};

Then in the construtor of A, you add this to the vector. In the destructor, you remove it.

Then, with your class B, you can look for the instance of A you need and use it:

B->ptr = A::arrA[0];


This is the kind of thing you can use. Even if you don't want to nake all members of A static, you can just create some static members for specific purposes.