Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Two classes each pointing to each other

Posted on 2003-03-15
3
Medium Priority
?
180 Views
Last Modified: 2010-04-01
I have two classes and want each to have a pointer to an instance of another.  When I try to add a pointer to the second class I get lots of errors.  How do I do this?
0
Comment
Question by:eamarks
  • 2
3 Comments
 
LVL 2

Expert Comment

by:limestar
ID: 8144070
Like this, naming the classes A and B as an example:

class A;

class B {
  A *a;
};

class A {
  B *b;
};

The first "class A;" is called a forward declaration of the A class.
0
 

Author Comment

by:eamarks
ID: 8144205
How can i make sure the right class is a forward declaration when they are all defined in header files.  The situation looks like this:


MAIN FILE

#include "Class1"
#include "Class2"


Class1 myClass1;
Class2 myClass2;




myClass1.myClass2 = &myClass2;
myClass2.myClass3.myClass1 = &myClass1;


CLASS 1 HEADER
#include "Class2"

class Class1  
{
public:

Class2* myClass2;

};


CLASS 2 HEADER
#include "Class1"
#include "Class3"

class Class2
{
public:

Class3 myClass3;

};

CLASS 3 HEADER
#include "Class1"

class Class3
{
public:

Class1* myClass1;

};


If you want I can send you the code and you can check it out - it's a pretty large program and unfortunately I don't have any more points in my account to offer.
0
 
LVL 2

Accepted Solution

by:
limestar earned 80 total points
ID: 8144284
If i understood the followup question correctly, the problem now was that the classes are declared in different header files? If so, forward declare the classes and include the other header at the end. Like this with two classes each in its own header:

--- a.h ---

#ifndef _a_h
#define _a_h

class B;

class A {
    B *b;
};

#include "b.h"
#endif

--- b.h ---

#ifndef _b_h
#define _b_h

class A;

class B {
    A *a;
};

#include "a.h"
#endif

--- end ---

That way you've made sure that you always get full declarations of both classes but always forward declare the one that gets declared last. Be careful with the include guards as well, since you'll end up with an infinite recursion if you don't place them right.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question