Click to See Complete Forum and Search --> : circular references
Mary
March 29th, 1999, 07:34 PM
Hi,
How can a use a circular reference in Visual C++? I want to create a pointer
of one class, classA, to point to another class, classB, (classB* ptrB) and set up a pointer from classB to point at classA (classA* ptrA). However, I can't
seem include classB in classA as well as including classA in classB. In Object
Pascal one can do this by including the other class in the variable uses clause.
Thanks for your help
mary
March 29th, 1999, 07:34 PM
Hi,
How can a use a circular reference in Visual C++? I want to create a pointer
of one class, classA, to point to another class, classB, (classB* ptrB) and set up a pointer from classB to point at classA (classA* ptrA). However, I can't
seem include classB in classA as well as including classA in classB. In Object
Pascal one can do this by including the other class in the variable uses clause.
Thanks for your help
phil
March 29th, 1999, 07:48 PM
Use a forward declaration. OO purest forgive me for leading someone so young astray.
class B; //Just a promise to the compiler that you will define a class B sooner or later;
Class A : public CObject
.
.
public:
B* pB; //A pointer to class B, the compiler will not mind because you already
//promised to define a class B.
}; // end of class A
Class B
{
}; // end of class b
Then, in the constructor or OnInit of class B just
pB = this;
Good luck.
Phil
March 29th, 1999, 07:48 PM
Use a forward declaration. OO purest forgive me for leading someone so young astray.
class B; //Just a promise to the compiler that you will define a class B sooner or later;
Class A : public CObject
.
.
public:
B* pB; //A pointer to class B, the compiler will not mind because you already
//promised to define a class B.
}; // end of class A
Class B
{
}; // end of class b
Then, in the constructor or OnInit of class B just
pB = this;
Good luck.
Mary
March 31st, 1999, 09:37 AM
Yes, but I didn't want the two classes in the same unit.
mary
March 31st, 1999, 09:37 AM
Yes, but I didn't want the two classes in the same unit.
phil
March 31st, 1999, 10:24 PM
Sorry Mary. I guess I do not understand what you mean by "same unit". If same unit means same source file, then there is no problem. A forward reference just requires a single h file in common for both proto-types.
Phil
March 31st, 1999, 10:24 PM
Sorry Mary. I guess I do not understand what you mean by "same unit". If same unit means same source file, then there is no problem. A forward reference just requires a single h file in common for both proto-types.
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.