2 way association

This is a discussion on 2 way association within the C++ Programming forums, part of the General Programming Boards category; How do I implement a binary association between 2 classes? Like this: Code: Class A { private: B *m_bRef; public: ...

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    29

    2 way association

    How do I implement a binary association between 2 classes?

    Like this:
    Code:
    Class A
    {
    private:
      B *m_bRef;
    public:
      A(void);
    };
    Code:
    Class B
    {
    private:
      A *m_aRef;
    public:
      B(void);
    };
    when A is in a.hpp and B is in b.hpp

    I tried with: [ Compiles (VC++) but instantly crashes ]
    Code:
    #include <string>
    #include <iostream>
    
    namespace Test
    {
    	class B;
    	class A
    	{
    		friend class B;
    	public:
    		B *m_bRef;
    		int m_int;
    
    	public:
    		A(int _int);
    		int getBInt(void);
    	};
    
    	class B
    	{
    		friend class A;
    	public:
    		A *m_aRef;
    		int m_int;
    
    	public:
    		B(int _int);
    		int getAInt(void);
    	};
    }
    
    Test::A::getBInt(void)
    {
    	return this->m_bRef->m_int;
    }
    Test::B::getAInt(void)
    {
    	return this->m_aRef->m_int;
    }
    Test::A::A(int _int)
    {
    	this->m_int = _int;
    }
    Test::B::B(int _int)
    {
    	this->m_int = _int;
    }
    
    int main()
    {
    	Test::A a(25);
    	Test::B b(100);
    
    	std::cout << a.getBInt();
    	std::cout << b.getAInt();
    
    	std::cin.get();
    
    	return 0;
    }

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Well, you create an instance of class A and class B, but you never initialize the pointers to point to each other. That's the problem. When you call the getAInt and getBInt functions, you are accessing uninitialized pointers. Fix that and see if it works.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    29
    like this ?
    Code:
    #include <string>
    #include <iostream>
    
    namespace Test
    {
    	class B;
    	class A
    	{
    		friend class B;
    	private:
    		B *m_bRef;
    		int m_int;
    
    	public:
    		A(int _int);
    		int getBInt(void);
    		void setB(B *_b);
    	};
    
    	class B
    	{
    		friend class A;
    	private:
    		A *m_aRef;
    		int m_int;
    
    	public:
    		B(int _int);
    		int getAInt(void);
    		void setA(A *_a);
    	};
    }
    
    Test::A::A(int _int)
    {
    	this->m_int = _int;
    	this->m_bRef = 0;
    }
    Test::A::getBInt(void)
    {
    	return this->m_bRef->m_int;
    }
    void Test::A::setB(Test::B *_b)
    {
    	this->m_bRef = _b;
    }
    
    Test::B::B(int _int)
    {
    	this->m_int = _int;
    	this->m_aRef = 0;
    }
    Test::B::getAInt(void)
    {
    	return this->m_aRef->m_int;
    }
    void Test::B::setA(Test::A *_a)
    {
    	this->m_aRef = _a;
    }
    
    using namespace Test;
    int main()
    {
    	A *a = new A(25);
    	B *b = new B(100);
    
    	a->setB(b);
    	b->setA(a);
    
    	std::cout << a->getBInt() << std::endl;
    	std::cout << b->getAInt() << std::endl;
    
    	std::cin.get();
    
    	return 0;
    }
    Output :
    100\n25\n

    the only problem i have with this is that I'm separating code into declaration and definition parts (.hpp & .cpp parts )

    Can I do it all in one piece in any way ?

  4. #4
    Tropical Coder Darryl's Avatar
    Join Date
    Mar 2005
    Location
    Cayman Islands
    Posts
    503
    Can I do it all in one piece in any way ?
    No, at least one of the classes must be separate, you can't use a pointer before it's defined which would be the case if you defined the class members in the declaration. For example:

    Code:
    struct bar; // forward declaration
    struct foo
    {
    	bar* pBar;
    	foo()
    	{
    		pBar->value = 10; // <------ERROR, can't use before definition(below)
    	}
    };
    
    struct bar
    {
    	int value;
    };
    
    foo::foo()
    {		
    	pBar->value = 10; // <------OK, has been defined (above)
    }

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    29
    What are the Cons & Pros of having this OO Design ?

  6. #6
    Registered User
    Join Date
    Oct 2005
    Posts
    29
    Anyone?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Struct Association
    By leandroaz in forum C Programming
    Replies: 8
    Last Post: 10-01-2008, 08:03 AM
  2. File association
    By patrick22 in forum Windows Programming
    Replies: 2
    Last Post: 05-27-2008, 03:53 PM
  3. install linux in association with windows
    By Yarin in forum Tech Board
    Replies: 8
    Last Post: 04-06-2008, 06:07 PM
  4. How to create a file association program?
    By eShain in forum Windows Programming
    Replies: 1
    Last Post: 03-06-2006, 11:15 AM
  5. File Association
    By Labelizm in forum Windows Programming
    Replies: 3
    Last Post: 07-24-2002, 02:10 AM

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