Inter-dependant classes

This is a discussion on Inter-dependant classes within the C++ Programming forums, part of the General Programming Boards category; I have 3 classes each of which is related to the other 2 in some way. The problem is to ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Inter-dependant classes

    I have 3 classes each of which is related to the other 2 in some way. The problem is to define, lets say, the first class; I have to first define the class that appears in it. IE. to define UnitInstance, I have to first define SetOfOrders. But to define SetOfOrders I have to first define Order. But to define Order, I have to define UnitInstance. And so on and so forth....

    I originally had these in seperate files, but the same problem occurs (It's just a lot harder to see).

    This code appears in one of the headers:

    Code:
    class UnitInstance {
    public:
    	SetOfOrders UnitOrders;
    	// Other variables & function prototypes.
    };
    
    
    class Order {
    public:
    	UnitInstance *Unit1;
    	// Other variables & function prototypes.
    };
    
    
    class SetOfOrders {
    public:
    	Order *Orders;
    	// Other variables & function prototypes.
    };

    Note that in the Order class, Unit1 is a pointer to another entirely different UnitInstance (or may even be null) - so it's not as if I was trying to allocate memory in cycle.

    If it makes any difference I'm using Microsoft Visual C++ 6.0 Pro.

    How should I fix this? Is there a keyword I should be using - should I be arranging these classes in a certain order - are there any compiler options I should be setting?

  2. #2
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    You can declare classes, just like you declare functions, before you define them. Example:

    Code:
    class a;
    class b;
    class c;
    
    class a
    {
    	public: b *b1;
    };
    class b
    {
    	public: c *c1;
    };
    class c
    {
    	public: a *a1;
    };

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    You need to use a forward declaration.
    Code:
    class Order;
    
    class SetOfOrders {
    public:
    	Order *Orders;
    	// Other variables & function prototypes.
    };
    
    class UnitInstance {
    public:
    	SetOfOrders UnitOrders;
    	// Other variables & function prototypes.
    };
    
    
    class Order {
    public:
    	UnitInstance *Unit1;
    	// Other variables & function prototypes.
    };

  4. #4
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Thanks a lot for that. Part of me knew it was that easy......

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    And those forward declarations will save you a lot of headache. I personally think they are clunky and ugly, but they do work and since C++ has no other way of dealing with the problem of A needs B but B needs A, we are stuck with it.

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    In your header files, you should prefer forward declarations over #including the headers that declare the other class if it will work.

  7. #7
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    I might be mistaken but forward declaration only works in the case that the header being defined only uses pointers to the other needed class. Take for instance:
    Code:
    //forward declaration
    class classA;
    
    class classB
    {
    protected:
        classA *m_ptr;
    
    public:
    
    ...
    
    };
    This will work since you define only a ptr to classA so classA object size is not important since you won't be allocating a classA object on stack but only a pointer to it and a pointer is always the same size, if on the other hand you had a class:
    Code:
    //forward declaration
    class classA;
    
    class classB
    {
    protected:
        classA m_obj;
    
    ...
    };
    Now this wouldn't work without including the header file for classA since for creating an instance of classB you'll need to allocate space for an instance of classA.

    Cheers

  8. #8
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    You are correct, although there are other cases besides pointer members where a forward declaration will do, including reference members and function parameters.

  9. #9
    #define WORLD "sad place" LinuxCoder's Avatar
    Join Date
    Mar 2006
    Location
    Portugal
    Posts
    89
    I thought so, and thanks for the correction there Daved

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can you Initialize all classes once with New?
    By peacerosetx in forum C++ Programming
    Replies: 12
    Last Post: 07-02-2008, 11:47 AM
  2. im extreamly new help
    By rigo305 in forum C++ Programming
    Replies: 27
    Last Post: 04-24-2004, 12:22 AM
  3. Exporting VC++ classes for use with VB
    By Helix in forum Windows Programming
    Replies: 2
    Last Post: 12-29-2003, 05:38 PM
  4. Prime Number Generator... Help !?!!
    By Halo in forum C++ Programming
    Replies: 9
    Last Post: 10-20-2003, 08:26 PM
  5. include question
    By Wanted420 in forum C++ Programming
    Replies: 8
    Last Post: 10-17-2003, 04:49 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21