TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTIAN S. PEDERSEN
from: JERRY COFFIN
date: 1997-07-24 09:25:00
subject: Mutually class definition.

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> }
 CS> two classes which each contains a function which return a instance of
 CS> the other class. I get a "typename expected"-error in class A. I
 CS> believe I have to use the "typename" keyword, but I can't get it to
 CS> work.
Nope - you just have to do a forward declaration that B is the name of a
class before you try to use it as a return type:
class B;
class A {
    B func1() { return B(1); }
};
class B {
    B(int a) { /* ... */ }
};
should do the trick.
    Later,
    Jerry.
... The Universe is a figment of its own imagination.
--- PPoint 1.90
---------------
* Origin: Point Pointedly Pointless (1:128/166.5)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.