How do I implement a binary association between 2 classes?
Like this:
Code:Class A { private: B *m_bRef; public: A(void); };when A is in a.hpp and B is in b.hppCode:Class B { private: A *m_aRef; public: B(void); };
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; }