Like Tree1Likes
  • 1 Post By Elysia

Two objects that has a pointer to each other

This is a discussion on Two objects that has a pointer to each other within the C++ Programming forums, part of the General Programming Boards category; I need to have two objects that has a pointer to each other. Code: class A { ..... B* pointer_to_B; ...

  1. #1
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265

    Two objects that has a pointer to each other

    I need to have two objects that has a pointer to each other.
    Code:
    class A
    {
          .....
          B* pointer_to_B;
    }
    Code:
    class B
    {
          .....
          A* pointer_to_A;
    }
    It seems like it's not possible since one of them can't know about the other cuz of the include...

    I find it really odd if it can't be done.

    Thanks! (:
    gavra.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,714
    Use forward declarations, e.g.,
    Code:
    class B;
    
    class A
    {
          .....
          B* pointer_to_B;
    };
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Thanks, assumed there's something like this, but it still doesn't work:

    CountsNode.h
    Code:
    #ifndef COUNTS_NODE_H
    #define COUNTS_NODE_H
    #include "RBNode.h"
    #include "CNode.h"
    
    class CNode;
    
    class CountsNode : public RBNode
    {
    	private:
    		int num_of_books;
    		CNode* customer;
    
    
    	public:
    		CountsNode(CNode* customer)
    		{
    			this->num_of_books = 1;
    			this->customer = customer;
    		};
    };
    #endif
    CNode.h
    Code:
    #include "RBNode.h"
    #include "Customer.h"
    
    class CNode : public RBNode
    {
    	private:
    		Customer* customer; // customer info object
    
    	public:
    		CNode(Customer* customer);
    		int GetKey() {return this->customer->id;};
    		void Print();
    		void CopyTo(RBNode* node);
    };
    Customer.h
    Code:
    #ifndef CUSTOMER_H
    #define CUSTOMER_H
    #include <iostream>
    #include <string>
    #include "RBNode.h"
    #include "CountsNode.h"
    
    using namespace std;
    
    class CountsNode;
    
    class Customer
    {
    	public:
    			static const int MAX_BOOKS = 10;
    			Customer(int id, string familyName);
    			int GetCount() {return (this->count == NULL ? 0 : this->count->GetKey());};
    			
    	private:
    			int id;
    			CountsNode* count;
    			string name;
    
    	friend class CNode;
    };
    #endif
    I think the problem is with the includes (it's recursive). If I remove the include of CNode.h from CountsNode.h the build succeed but I can't it says that objects customer has no properties (which is logical since I didn't include it, just declare).
    gavra.

  4. #4
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    Forward declarations are fine, but you still can't use the forward declared classs in inline functions. If you move the implementation of the constructor CountsNode(CNode* customer) into the implementation file ( .cpp ) then it works fine.

    Kurt

  5. #5
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    I removed the include of CNode.h in CountsNode.h and moved the implementation just as you said but still 'No memebers' for customer... /:
    gavra.

  6. #6
    ZuK
    ZuK is offline
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    You have to #include "CNode.h" in CountsNode.cpp and implement the constructor there.
    Kurt

  7. #7
    Registered User gavra's Avatar
    Join Date
    Jun 2008
    Posts
    265
    Great. Thank you!
    gavra.

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,587
    Btw, consider using smart pointers if you are using pointers.
    Elkvis likes this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Cat
    Cat is offline
    Registered User
    Join Date
    May 2003
    Posts
    1,571
    Also, I'd make sure that you really should be structuring the code like this, or if there's a better way. Sometimes this scenario comes up, but often there's a better way to accomplish what you need without tightly coupling these two classes.

    Some design choices that can help in some cases are the creation of abstract interfaces, and the observer pattern. The latter is particularly useful when you have objects that have an owner / owned relationship and the owner needs to be informed about certain changes to the owned object. Rather than the owned object needing to know who its owner is, the owner registers itself as a listener for events, and the owned object just broadcasts its state change to anyone who cares.

    An example - suppose you have data objects that have a UID and other associated data, and you've got a collection class which is storing these in some kind of key-value container structure to look up objects by UID. If the data object has a method that allows its UID to change, the container object needs to know because it probably needs to re-key the data internally. You *could* make the data object aware of its container object, but that limits reuse of the data object. A better way would be to make the data object observable and let the container object register itself as an observer if it needs to. That makes the data object more easily reused.
    You ever try a pink golf ball, Wally? Why, the wind shear on a pink ball alone can take the head clean off a 90 pound midget at 300 yards.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with pointer to vector objects
    By ryeguy in forum C++ Programming
    Replies: 5
    Last Post: 02-11-2008, 06:42 AM
  2. new Array of pointer objects
    By Death_Wraith in forum C++ Programming
    Replies: 5
    Last Post: 05-04-2004, 12:58 PM
  3. array of pointer objects
    By Death_Wraith in forum C++ Programming
    Replies: 2
    Last Post: 04-09-2004, 07:06 PM
  4. Using a smart pointer for all objects
    By phalc in forum C++ Programming
    Replies: 1
    Last Post: 03-28-2004, 08:23 PM
  5. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM

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