?
Solved

Two classes each pointing to each other

Posted on 2003-03-15
3
Medium Priority
?
178 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses
Course of the Month8 days, 8 hours left to enroll

765 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