A constructor question

J

Jorn Attermann

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

Guest

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.


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

jeffc

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

Rolf Magnus

jeffc said:
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).
 
J

jeffc

Rolf Magnus said:
Yes, necessarily.


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;
}
 
J

jeffc

Rolf Magnus said:
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.
 
K

Karl Heinz Buchegger

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

Rolf Magnus

jeffc said:
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.
 
J

jeffc

Karl Heinz Buchegger said:
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.
 
J

Jorn Attermann

Karl Heinz Buchegger said:
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.

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
 

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

Forum statistics

Threads
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top