How to handle mutually dependent classes

B

Brad Kartchner

In a project I am working on, I have found myself with two classes that
reference each other. For example:

firstclass.h:
{
class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h
{
class SecondClass
{
function (FirstClass* Pointer);
}
}

Sadly, I can't think of a better solution for what I am trying to do.
Unfortunately, I come up with the obvious problem that each class needs the
other one to be declared BEFORE it can be declared itself.

Is there any elegant way to handle this, or do I go back to the drawing
board and think up a better solution?

Brad Kartchner
 
J

JKop

Brad Kartchner posted:
In a project I am working on, I have found myself with two classes that
reference each other. For example:

firstclass.h:
{
class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h
{
class SecondClass
{
function (FirstClass* Pointer);
}
}

Sadly, I can't think of a better solution for what I am trying to do.
Unfortunately, I come up with the obvious problem that each class needs
the other one to be declared BEFORE it can be declared itself.

Is there any elegant way to handle this, or do I go back to the drawing
board and think up a better solution?

Brad Kartchner


class B;

class A
{
B monkey;
}


class B
{
A ape;
}



TTTTTTThhhhhhhhheeeeeeerrrrreeeeeeeee ya go


-JKop
 
S

Siemel Naran

Brad Kartchner said:
In a project I am working on, I have found myself with two classes that
reference each other. For example:

Use forward declarations.
firstclass.h:

class SecondClass;
// this line declare that there exists class named SecondClass
// it will be defined later
// but at least you can declare pointers and reference to SecondClass
{
class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h

class FirstClass; // add a forward declaration here too
 
S

Siemel Naran

JKop said:
class B;

class A
{
B monkey;
}

The above is invalid. The compiler needs to know the sizeof(A), and in
order to do this in needs to know sizeof(B). But the class is not declared.
Hence a compile error. The best you can do is

class A
{
B * monkey;
};

Of course, feel free to use smart pointers instead.
 
K

Karthik

Brad said:
In a project I am working on, I have found myself with two classes that
reference each other. For example:

firstclass.h:
{
class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h
{
class SecondClass
{
function (FirstClass* Pointer);
}
}

Sadly, I can't think of a better solution for what I am trying to do.
Unfortunately, I come up with the obvious problem that each class needs the
other one to be declared BEFORE it can be declared itself.

Is there any elegant way to handle this, or do I go back to the drawing
board and think up a better solution?

Brad Kartchner
Step 1: Think if you really need that scenario in the first place. Avoid
them if you can.

Step 2: If you can't do without them, follow this -

firstclass.h:

class SecondClass;
// Putting a word to the linker that you
// will find the definition of the SecondClass eventually.

class FirstClass
{
function (SecondClass* Pointer);
}

secondclass.h

class SecondClass
{
function (FirstClass* Pointer);
}
 
A

Andre Kostur

JKop said:
Brad Kartchner posted:



class B;

class A
{
B monkey;
}


class B
{
A ape;
}



TTTTTTThhhhhhhhheeeeeeerrrrreeeeeeeee ya go

Uh, no. Please tell me what value sizeof(B) has on your compiler (and
just to make it interesting, add an "int apeAge;" to class A)....

For the OP: look up forward-declarations. Example:

firstclass.h:
{
class SecondClass;

class FirstClass
{
function (SecondClass* Pointer);
}
}

secondclass.h
{
class FirstClass;

class SecondClass
{
function (FirstClass* Pointer);
}
}
 
B

Brad Kartchner

Step 1: Think if you really need that scenario in the first place. Avoid
them if you can.

Step 2: If you can't do without them, follow this -

firstclass.h:

class SecondClass;
// Putting a word to the linker that you
// will find the definition of the SecondClass eventually.

class FirstClass
{
function (SecondClass* Pointer);
}

secondclass.h

class SecondClass
{
function (FirstClass* Pointer);
}

Thanks. It seems so obvious... I will keep trying to find a better
implementation, but I'll use this for now.

Brad Kartchner
 
D

David Harmon

On Fri, 30 Apr 2004 14:16:03 -0600 in comp.lang.c++, "Brad Kartchner"
Unfortunately, I come up with the obvious problem that each class needs the
other one to be declared BEFORE it can be declared itself.

This issue is covered in Marshall Cline's C++ FAQ. See the topic
"[38.11] How can I create two classes that both know about each other?"
It is always good to check the FAQ before posting. You can get the FAQ
at:
http://www.parashift.com/c++-faq-lite/
 
J

JKop

class B;
class A
{
public:
B monkey;
}


class B
{
public:
A ape;
}


Been thinking about this.

Have you ever held a small mirror up to a big full length mirror, and then
looked into the full length mirror? You see an image of the small mirror,
and inside it is the image of the full length mirror, and inside that is the
image of the small mirror, and inside that is the image of the full length
mirror, and inside that is the image of the small mirror, and inside that is
the image of the full length mirror, and inside that is the image of the
small mirror, and inside that is the image of the full length mirror, and
inside that is the image of the small mirror, and inside that is the image
of the full length mirror, and inside that is the image of the small mirror,
and inside that is the image of the full length mirror... and on and on to
eternity.

Well that's what's happening with my code above.

When you declare a class A:

A Bubbles;

That Bubbles contains a B, and that B contains an A, and that A contains a
B, and that B contains an A, and that A contains a B, and that B contains an
A, and that A contains a B, and that B contains an A, and that A contains a
B, and that B contains an A, and that A contains a B, and that B contains an
A, and that A contains a B, and that B contains an A, and that A contains a
B, and that B contains an A, and that A contains a B, and that B contains an
A, and that A contains a B, and that B contains an A, and that A contains a
B, and that B contains an A, and that A contains a B, and that B contains an
A, and that A contains a B, and that B contains an A, and that A contains a
B, and that B contains an A, and that A contains a B, and that B contains an
A, and that A contains a B, and that B contains an A, and that A contains a
B, and that B contains an A, and that A contains a B, and that B contains an
A... and on and on to enternity.

sizeof(A) = Infinity

sizeof(B) = Inifinity


Other than that, to declare the A class, you've to know the specifics of the
B class, so define the B class first. But then the B class needs to know the
specifics of the A class, so define the A class first. But then the A class
needs to know the specifics of the B class, so define the B class first. But
then the B class needs to know the specifics of the A class, so define the A
class first. But then the A class needs to know the specifics of the B
class, so define the B class first. But then the B class needs to know the
specifics of the A class, so define the A class first. But then the A class
needs to know the specifics of the B class, so define the B class first...


-JKop
 
W

Walter Tross

Been thinking about this.

Have you ever held a small mirror up to a big full length mirror, and then
looked into the full length mirror? You see an image of the small mirror,
and inside it is the image of the full length mirror, and inside that is the
[snip snip snip snip snip snip...]

keep it short, JKop, that code will never compile.
Class A can't contain class B, since class B has not been defined yet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top