Advertisement

Class instantiation

Started by February 28, 2000 04:02 PM
1 comment, last by HOOKUPS 24 years, 6 months ago
I got a problem with a program im doing!!! Ive got 2 classes which points to one another. But the problem is that one of the classes is instanced before the other therefore they cant point to the one that hasnt been instanced yet.... i know u can work around this my question is how ??? Thanx
Use a forward declaration.

For example, A.h might look like this:

class B;

class A {
B* pointer_to_B;
};


And B.h might look like this:

class A;

class B {
A* pointer_to_A;
};


Then in A.cpp you would do something like this:

#include "A.h"
#include "B.h"

A::A( B *b ) : pointer_to_B( b )
{
pointer_to_B->functionCall();
}


You can''t actually do anything with your pointers until
you''ve including the class header but this will get you
around the problem you are having. You should make a
practice of using forward declarations in your header
files as it helps decrease compile time.


Advertisement
Thanx

This topic is closed to new replies.

Advertisement