|
-
March 29th, 1999, 08:34 PM
#1
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
-
March 29th, 1999, 08:48 PM
#2
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.
-
March 29th, 1999, 08:48 PM
#3
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.
-
March 31st, 1999, 10:37 AM
#4
Re: circular references
Yes, but I didn't want the two classes in the same unit.
-
March 31st, 1999, 11:24 PM
#5
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.
-
March 31st, 1999, 11:24 PM
#6
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|