Thread: Declaring classes and defining later?

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    65

    Declaring classes and defining later?

    Is it possible to declare classes and define them later?
    Such as for this code
    Code:
    class foo {
    	bar b;
    };
    
    class bar {
    	foo f;
    };

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    that's called forward declaration.

    Code:
    class foo;
    
    class bar
    {
            foo *f;
    };

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by jw232 View Post
    Is it possible to declare classes and define them later?
    Such as for this code
    Code:
    class foo {
    	bar b;
    };
    
    class bar {
    	foo f;
    };
    This example will not work, because foo contains bar, bar contains foo that contains bar that contains foo that contains bar - it will go on forever - the compiler can't deal with that (nor can I, really - I don't quite know when to stop repeating it).

    You can have a reference or pointer to one of the classes in the other, but you can't mutually put the entire class inside another class that wholly contains the previous class - it's never going to resolve the content of the class.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    Forward declaration may help:

    Code:
    class bar;
    
    class foo { 
       bar b;
     };
    
    class bar {
       foo f;
      // whatever
    };
    I would ask that you carefully consider foo's and bar's relationship though, to avoid a circular logic problem. Does one control or own another object, for instance? You will want to seek another type of member storage, so that foo can be created without immediately instantiating a bar, which needs a foo, which needs a bar, which needs a foo ...

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    Code:
    //Forward declaration
    class bar;
    
    class foo
    {
       bar* b;
    }
    
    class bar
    {
       foo* f;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Defining multiple classes in the same header file
    By Stonehambey in forum C++ Programming
    Replies: 2
    Last Post: 08-14-2008, 10:36 AM
  2. Replies: 4
    Last Post: 07-10-2006, 02:44 AM
  3. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-23-2004, 11:22 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 07:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 03:49 AM