-
circular references
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
-
Re: circular references
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.
-
Re: circular references
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.
-
Re: circular references
Yes, but I didn't want the two classes in the same unit.
-
Re: circular references
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.
-
Re: circular references
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.