473,394 Members | 1,944 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to handle mutually dependent classes

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
Jul 22 '05 #1
10 3147
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
Jul 22 '05 #2
JKop <NU**@null.null> spoke thus:
class B;


For OP's benefit, this is called a forward declaration.

--
Christopher Benson-Manica | I *should* know what I'm talking about - if I
ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
Jul 22 '05 #3
"Brad Kartchner" <la***********@qwest.net> wrote in message news:gvykc.37
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
{
class SecondClass
{
function (FirstClass* Pointer);
}
}


Jul 22 '05 #4
"JKop" <NU**@NULL.NULL> wrote in message news:RFykc.6187
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.
Jul 22 '05 #5
Brad Kartchner wrote:
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);
}

--
Karthik
Humans please 'removeme_' for my real email.
Jul 22 '05 #6
JKop <NU**@NULL.NULL> wrote in news:RF******************@news.indigo.ie:
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


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);
}
}
Jul 22 '05 #7
> 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);
}

--
Karthik
Humans please 'removeme_' for my real email.


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

Brad Kartchner
Jul 22 '05 #8
On Fri, 30 Apr 2004 14:16:03 -0600 in comp.lang.c++, "Brad Kartchner"
<la***********@qwest.net> wrote,
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/

Jul 22 '05 #9
> 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
Jul 22 '05 #10
On Sat, 01 May 2004 11:14:21 GMT, JKop <NU**@NULL.NULL> wrote:
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

[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.

Jul 22 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Doug Baroter | last post by:
Hi, DDL: -- create table #task (taskID int identity(1,1) primary key, taskName varchar(25) unique, taskCompleteDate dateTime, taskComplete bit default(0)); /* Business Rules: a) if...
5
by: Colin JN Breame | last post by:
Say there are two classes: class A { B *b; }; class B { A *a; };
2
by: Indiana Epilepsy and Child Neurology | last post by:
Before asking this questions I've spent literally _years_ reading (Meyer, Stroustrup, Holub), googling, asking more general design questions, and just plain thinking about it. I am truly unable to...
14
by: G Patel | last post by:
Pg. 140 of K&R2 shows an example of mutually referential structure declarations... struct t { struct s *p; }; struct s {
2
by: mscertified | last post by:
I have 2 drop-downs on my web form. I need to make the contents of the second one dependent on the choice from the first. How? I tried removing the existing entries from the second listbox and...
6
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in...
2
weaknessforcats
by: weaknessforcats | last post by:
Handle Classes Handle classes, also called Envelope or Cheshire Cat classes, are part of the Bridge design pattern. The objective of the Bridge pattern is to separate the abstraction from the...
4
by: redqil | last post by:
The problem I am facing is that an integer is the ideal representation for many different classes I am using. I want to write overloaded functions that are able to differentiate between the classes...
5
by: Matthew Wilson | last post by:
I started off with a module that defined a class Vehicle, and then subclasses Car and Motorcycle. In the Car class, for some bizarre reason, I instantiated a Motorcycle. Please pretend that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.