Objects pointing to each other

This is a discussion on Objects pointing to each other within the C++ Programming forums, part of the General Programming Boards category; I have two classes, and I want each to contain a pointer to the other. How do I do this? ...

  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,211
    Code:
    class b;
    
    class a{
    	b* pnt;
    };
    
    class b{
    	a* pnt;
    };

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,317
    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, 10: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

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