#### Archived

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

# C++ Class question

This topic is 6271 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

If you have:
  class FOO { public: BAR MyBar; } class BAR { public: FOO MyFoo; } 
as soon as the compiler hits:
  BAR MyBar; 
It will crap out cause class BAR hasn''t been defined yet. Is there some way how I could define my classes at the top kinda like you define functions at the top so that one can call another thats below it in your code?

##### Share on other sites
That wouldn''t compile because you are missing semicolons, but assuming you fixed that, it still would not compile because you would create an endless loop of FOOS and BARS. The compiler sees the discrepency and will not compile. It *might* be possible to get the classes to define properly so you could do this, but the program would just crash at that line.

##### Share on other sites
You can prototype a class if it''s not actually instantiated before it''s defined, so for your example you couldn''t.

If you did

class Bar;

class FOO
{
public:
BAR *MyBar;
}
class BAR
{
public:
FOO MyFoo;
}

that would be OK, because MyBar is only a pointer to BAR and the compiler doesn''t need to know its size. Your original example, if it compiled, would recursively produce a structure of infinite size.

##### Share on other sites
just a note, still missing the semicolons on that last example by Ruval, but that''s the correct answer.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 12
• 16
• 10
• 14
• 10
• ### Forum Statistics

• Total Topics
632659
• Total Posts
3007692
• ### Who's Online (See full list)

There are no registered users currently online

×