Thread: Objects pointing to each other

  1. #1
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40

    Objects pointing to each other

    I have two classes, and I want each to contain a pointer to the other. How do I do this?

    (the first is declared, but the 2nd hasn't been, so the compiler doesn't know the type that the pointer is pointing to)

    To show what I mean:
    Code:
    class a{
    	b* pnt;
    };
    
    class b{
    	a* pnt;
    };

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Code:
    class b;
    
    class a{
    	b* pnt;
    };
    
    class b{
    	a* pnt;
    };

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    It's called a forward declaration. You should generally use instead of an include when you can, even if you don't have the circular reference.

  4. #4
    cout << "Bye World!";
    Join Date
    Jun 2006
    Posts
    40
    Cool. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 60
    Last Post: 12-20-2005, 11:36 PM
  2. Question about cout an stack object?
    By joenching in forum C++ Programming
    Replies: 8
    Last Post: 05-08-2005, 10:10 PM
  3. chain of objects within pop framework help needed
    By Davey in forum C++ Programming
    Replies: 0
    Last Post: 04-15-2004, 10:01 AM
  4. Replies: 4
    Last Post: 10-16-2003, 11:26 AM
  5. array of objects?
    By *~*~*~* in forum C++ Programming
    Replies: 4
    Last Post: 05-31-2003, 05:57 PM