TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CHRISTIAN S. PEDERSEN
from: KURT KUZBA
date: 1997-07-27 14:01:00
subject: Mutually class definition

CS>   two classes which each contains a function which return a
CS>   instance of the other class. I get a "typename expected"-
CS>   error in class A. I believe I have use the "typename"
CS>   keyword, but I can't get it to work.
   This is called a circular dependency. :)
   The following code should compile and run with no errors:
#include 
class TypeB;
/*   This previous line does nothing but notify the compiler
      that there is, in fact, a valid class TypeB.
*/
class TypeA
{
public:
   void A(TypeB &a) { b = &a; };
   TypeB *func1() { return b; };
private:
   TypeB *b;
};
class TypeB
{
public:
   void A(TypeA &a) { b = &a; };
   TypeA *func1() { return b; };
private:
   TypeA *b;
};
int main(void)
{
   TypeA Acls;
   TypeB Bcls;
   Acls.A(Bcls);
   Bcls.A(Acls);
   cout << "Acls "
       << ((&Acls == Bcls.func1()) ? "==" : "!=")
       << " Bcls.func1()" << endl;
   cout << "Bcls "
       << ((&Bcls == Acls.func1()) ? "==" : "!=")
       << " Acls.func1()" << endl;
   return 0;
}
> ] ERROR: Demon not responding. Open pentagram to continue.....
---
---------------
* Origin: *YOPS ]I[* 3.1 GIG * RA/FD/FE RADist * Milwaukee, WI (1:154/750)

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