473,385 Members | 1,769 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,385 software developers and data experts.

A constructor question

I have a simple problem which I don't know how to handle. I have two
classes, X and Y, and in each I have a constructor which takes a reference
to the other as an argument as sketched below.

class X {
private:
int a;
public:
X();
X(Y&);
};
class Y {
private:
int b;
public:
Y();
Y(X&);
};
Naturally, the compiler complains because Y is undeclared when it parses the
definition of class X.

Any help is appreciated.

-----
Jorn Attermann,
Jul 19 '05 #1
10 1579

"Jorn Attermann" <jo****@nospam.dk> wrote in message news:3f***********************@dread11.news.tele.d k...
I have a simple problem which I don't know how to handle. I have two
classes, X and Y, and in each I have a constructor which takes a reference
to the other as an argument as sketched below.

add
class Y;
before the definition of X and vice versa.

http://www.parashift.com/c++-faq-lit...html#faq-38.11
Jul 19 '05 #2
On Tue, 14 Oct 2003 21:27:38 +0200, Jorn Attermann <jo****@nospam.dk>
wrote:
I have a simple problem which I don't know how to handle. I have two
classes, X and Y, and in each I have a constructor which takes a
reference
to the other as an argument as sketched below.

class X {
private:
int a;
public:
X();
X(Y&);
};
class Y {
private:
int b;
public:
Y();
Y(X&);
};
Naturally, the compiler complains because Y is undeclared when it parses
the
definition of class X.

Any help is appreciated.

-----
Jorn Attermann,

Forward declaration is needed.

class X;

class Y { declarations/ definitions ... };
class X { declarations/defintiions ... };

If , let's say , you want to use X as a member of Y , then X should be
defined first.
If reverse then reverse.
It's nonsense to have type X member in Y and type Y member in X at the
same type.

--
grzegorz
Jul 19 '05 #3

<gr**********@pacbell.net> wrote in message
news:op**************@news.sf.sbcglobal.net...

If , let's say , you want to use X as a member of Y , then X should be
defined first.
If reverse then reverse.
It's nonsense to have type X member in Y and type Y member in X at the
same type.


Not necessarily. I might put a box in car, or I might put a car in a box.
I might have a record that has a container, or I might have a container that
has a record.
Jul 19 '05 #4
jeffc wrote:

<gr**********@pacbell.net> wrote in message
news:op**************@news.sf.sbcglobal.net...

If , let's say , you want to use X as a member of Y , then X should
be
defined first.
If reverse then reverse.
It's nonsense to have type X member in Y and type Y member in X at
the same type.
Not necessarily.


Yes, necessarily.
I might put a box in car, or I might put a car in a box.
Right. The important part is _or_. Not every box contains a car and not
every car contains a box (that in turn would then contain a car, which
would contain a box, which .... you get the point).
I might have a record that has a container, or I might have a
container that has a record.

Jul 19 '05 #5

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bm*************@news.t-online.com...
jeffc wrote:

<gr**********@pacbell.net> wrote in message
news:op**************@news.sf.sbcglobal.net...

If , let's say , you want to use X as a member of Y , then X should
be
defined first.
If reverse then reverse.
It's nonsense to have type X member in Y and type Y member in X at
the same type.


Not necessarily.


Yes, necessarily.
I might put a box in car, or I might put a car in a box.


Right. The important part is _or_. Not every box contains a car and not
every car contains a box (that in turn would then contain a car, which
would contain a box, which .... you get the point).


Who says they have to be mutually exclusive? Let's just say for sake of
argument that all cars are normal size, but box size varies greatly. All
cars *can* have a box in them, and all boxes *can* have a car in them.

class Box;

class Car
{
private:
Box* b;
public:
Car():b(0) {}
Car(Box& box):b(&box){}
};

class Box
{
private:
int cubicFeet;
Car* c;
public:
Box(int size):cubicFeet(size) {}
Box(Car& car, int size):cubicFeet(size), c(&car){}
};

int main()
{
Car car1;
Box automobileShippingCrate(car1, 250);
Box containerForThings(1);
Car car2(containerForThings);

return 0;
}
Jul 19 '05 #6

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bm*************@news.t-online.com...

Right. The important part is _or_. Not every box contains a car and not
every car contains a box (that in turn would then contain a car, which
would contain a box, which .... you get the point).


grejdan said " It's nonsense to have type X member in Y and type Y member in
X at the same type."
Maybe he meant at the same "time". If so I see your point - yes, you can't
do a recursive inclusion like that.
Jul 19 '05 #7


gr**********@pacbell.net wrote:
If , let's say , you want to use X as a member of Y , then X should be
defined first.
If reverse then reverse.
It's nonsense to have type X member in Y and type Y member in X at the
same type.


Right.
But this is not what the OP is doing.
The OP is doing the equivalent of:
Every parent knows about its child and
every child knows about its parent.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 19 '05 #8
jeffc wrote:

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:bm*************@news.t-online.com...

Right. The important part is _or_. Not every box contains a car and
not every car contains a box (that in turn would then contain a car,
which would contain a box, which .... you get the point).
grejdan said " It's nonsense to have type X member in Y and type Y
member in X at the same type."
Maybe he meant at the same "time".


That's what I read anyway :-)
If so I see your point - yes, you can't do a recursive inclusion like
that.


I don't know if he meant that, but at least it was my point.

Jul 19 '05 #9

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


gr**********@pacbell.net wrote:

If , let's say , you want to use X as a member of Y , then X should be
defined first.
If reverse then reverse.
It's nonsense to have type X member in Y and type Y member in X at the
same type.


Right.
But this is not what the OP is doing.
The OP is doing the equivalent of:
Every parent knows about its child and
every child knows about its parent.


Huh? I don't understand your terminology. There is no subclassing involved
here.
Jul 19 '05 #10

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...


gr**********@pacbell.net wrote:

If , let's say , you want to use X as a member of Y , then X should be
defined first.
If reverse then reverse.
It's nonsense to have type X member in Y and type Y member in X at the
same type.


Right.
But this is not what the OP is doing.
The OP is doing the equivalent of:
Every parent knows about its child and
every child knows about its parent.

--
Karl Heinz Buchegger
kb******@gascad.at


I really appreciate the help from the group; it solved my problem. I also
find the other responses interesting and please let me explain what my
original problem was:

I have two classes (from Template Numerical Toolkit -
http://math.nist.gov/tnt/), an Array1D and an Array2D for numerical
computing. And I wanted to have a method for conversion between these. The
way I solved this was to include a constructor in each class which take as
argument a reference to the other class. So, there *is* actually a practical
use for it.

Thanks again,

Jorn Attermann
Jul 19 '05 #11

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

Similar topics

10
by: cppaddict | last post by:
Hi, I am writing a program and needs to know one of its object members before it can be initialized. It doesn't really matter for my question (which a C++ question, not a windows question), but...
24
by: slurper | last post by:
i have the following class sequence { public: sequence (const sequence& mysequence, const int newjob) { job_sequence(mysequence.job_sequence) job_sequence.push_back(newjob); ... }
6
by: Fred Zwarts | last post by:
Hello, I am trying to debug some complex debug code. In order to track the use of dynamically allocated memory, I replaced the standard global new and delete operators. (Not for changing the...
45
by: Ben Blank | last post by:
I'm writing a family of classes which all inherit most of their methods and code (including constructors) from a single base class. When attempting to instance one of the derived classes using...
2
by: david | last post by:
Well, as a matter of fact I_HAD_MISSED a basic thing or two, anyway, although Ollie's answer makes perfectly sense when dealing with classes, it doesn't seem to me to apply as well if you have to...
16
by: plmanikandan | last post by:
Hi, I have doubts reg virtual constructor what is virtual constructor? Is c++ supports virtual constructor? Can anybody explain me about virtual constructor? Regards, Mani
74
by: Zytan | last post by:
I have a struct constructor to initialize all of my private (or public readonly) fields. There still exists the default constructor that sets them all to zero. Is there a way to remove the...
19
by: Jeroen | last post by:
Hi guys, I have a simple question. If I have a class like: class A { A(); ~A(); A(A& a); A(int i);
1
by: Bob Johnson | last post by:
Please note that this question is NOT about the merits of Microsoft certification (no need to get into that here). While taking a MS certification exam, I came across a question where it was...
4
by: Rahul | last post by:
Hi Everyone, It is well known that the input parameter which is passed to the copy constructor is passed as reference and not as as object. Because passing an object is as good as making another...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...

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.