Archived

This topic is now archived and is closed to further replies.

PSioNiC

C++ Class question

Recommended Posts

(i thought i already posted this before but i can''t find it) If you have:
  

class FOO {
  public:
    BAR MyBar;
}

class BAR {
  public:
    FOO MyFoo;
}

  
the program will crash when it hits
  

BAR MyBar;

  
because the BAR class has not yet been defined. Is there some way how I could declare classes at the top (like a function declaration for classes), or is there another way around this?

Share this post


Link to post
Share on other sites
Well, you can use forward declarations:

  
class foo;
class bar;

class foo
{
...
};

class bar
{
...
};


However, in the example you gave, I don't see how the compiler could ever instantiate either of the objects. One depends on the other which depends on the one, and so forth. But if those members were pointers to foo and bar, that'd be fine.

Edited by - merlin9x9 on July 26, 2001 5:41:51 PM

Share this post


Link to post
Share on other sites