-=> Quoting Jerry Coffin to Christian S. Pedersen
JC> On (23 Jul 97) Christian S. Pedersen wrote to All...
CS> Hello All.
CS> I want to define two classes like the following:
CS> class A
CS> {
CS> A(int a) { };
CS> B func1() { return B(1); };
CS> }
CS> class B
CS> {
CS> B(int a) { };
CS> A func2() { return A(1); };
CS> }
I'd say you can't do it.
JC> Nope - you just have to do a forward declaration that B is the name of
JC> a class before you try to use it as a return type:
You can't return an incomplete type, Jerry.
JC> class B;
JC> class A {
JC> B func1() { return B(1); }
JC> };
JC> class B {
JC> B(int a) { /* ... */ }
JC> };
JC> should do the trick.
Nope. Expanding your code out to match Christian's code:
class B;
class A {
public:
A(int) {}
B func1() { return B(1); }
};
class B {
public:
B(int) {}
A func2(int a) { return A(1); }
};
This compiles as:
[0] e:\tmp\s>gcc -c t.cc
t.cc: In method `class B A::func1()':
t.cc:6: return-type `B' is an incomplete type
t.cc:6: type `B' is not yet defined
However, I'd propose that if you could get around this (I don't think you
can), you'd be given another (minor) roadblock: you don't know that B::B(int)
exists at that point. You'd have to move A::func1() to below class B's
declaration. In fact, this was what I originally expected, but obviously we
both missed the more important point.
You have to resort to making either A::func1() or B::func2() return a pointer
or reference (probably the former). Which means allocating memory on the
heap.
... Yes-men: Fellows who hang around the man nobody noes.
--- FastEcho 1.46
---------------
* Origin: House of Fire BBS - Toronto - (416)601-0085 - v.34 (1:250/536)
|