Create four classes. CMain, CData, CFirst,CSecond.
The class CData saved pointers to other classes. Through this class we can access to arbitrary classes.
#pragma once#include <iostream>usingnamespace std;class CFirst;class CSecond;class CMain;class CData{ CMain *m_pMain; ///< Pointer to main class CFirst *m_pFirst; ///< Pointer to first class CSecond *m_pSecond; ///< Pointer to second classpublic: CData(); ~CData();///\briefSetpointertomainclassvoid SetMain(CMain *_pMain) { m_pMain = _pMain; }///\briefGetpointertomainclass CMain *GetMain() {return m_pMain; }///\briefSetpointertofirstclassvoid SetFirst(CFirst *_pFirst) { m_pFirst = _pFirst; }///\briefGetpointertofirstclass CFirst *GetFirst() {return m_pFirst; }///\briefSetpointertosecondclassvoid SetSecond(CSecond *_pSecond) { m_pSecond = _pSecond; }///\briefGetpointertosecondclass CSecond *GetSecond() {return m_pSecond; }};
Class CFirst and CSecond contains pointer to global data CData *m_pData;.
#pragma once#include <iostream>usingnamespace std;#include "CData.h"#include "CMain.h"class CFirst{constchar* m_strName; ///< Class name CData *m_pData; ///< Global application datapublic: CFirst(CData *_pData); ~CFirst();void ShowData();///\briefGetfirstclassname///\retvalm_strName Class nameconstchar *GetName() {return m_strName; }};
#pragma once#include <iostream>usingnamespace std;#include "CData.h"#include "CMain.h"class CSecond{constchar* m_strName; ///< Class name CData *m_pData; ///< Global application datapublic: CSecond(CData *_pData); ~CSecond();void ShowData();///\briefGetsecondclassname///\retvalm_strName Class nameconstchar *GetName() {return m_strName; }};
In both classes set this pointer to global data. m_pData->SetFirst(this); m_pData->SetSecond(this);
CFirst::CFirst(CData *_pData){ cout << "CFirst::CFirst()" << endl; m_strName = "CFirst"; m_pData = _pData;// Set first class to global data m_pData->SetFirst(this);}
CSecond::CSecond(CData *_pData){ cout << "CSecond::CSecond()" << endl; m_strName = "CSecond"; m_pData = _pData;// Set second class to global data m_pData->SetSecond(this);}
Main class CMain contains pointers to other classes. CData *m_pData; CFirst *m_pFirst; CSecond *m_pSecond;
#pragma once#include <iostream>usingnamespace std;#include "CFirst.h"#include "CSecond.h"class CMain{constchar* m_strName; ///< Class name CData *m_pData; ///< Pointer to global application data CFirst *m_pFirst; ///< Pointer to first class CSecond *m_pSecond; ///< Pointer to second classpublic: CMain(); ~CMain();void ShowData();///\briefGetmainclassname///\retvalm_strName Class nameconstchar *GetName() {return m_strName; }};
In constructor of class CMain allocate global data m_pData = new CData();
Save this class pointer to global data m_pData->SetMain(this);
Allocate other classes and pass him pointer to global data m_pFirst = new CFirst(m_pData);
m_pSecond = new CSecond(m_pData);
CMain::CMain(){ cout << "CMain::CMain()" << endl; m_strName = "CMain"; m_pData = new CData();// Set main class to global data m_pData->SetMain(this);// Create first and second class m_pFirst = new CFirst(m_pData); m_pSecond = new CSecond(m_pData);}
Function ShowData()
obtaining class pointers from global data and show names of all classes.
///\briefShowallnamesofallclassesvoid CMain::ShowData(){// Access to all classes from first class m_pFirst->ShowData();// Access to all classes from second class m_pSecond->ShowData();}
///\briefShowallclassnamesfromglobaldatavoid CFirst::ShowData(){ cout << "Access from class CFirst" << endl;// Get main class from global data CMain *pMain = m_pData->GetMain();// Get main class nameconstchar* strName = pMain->GetName(); cout << "Main class name = " << strName << endl;// Get second class from global data CSecond *pSecond = m_pData->GetSecond();// Get second class name strName = pSecond->GetName(); cout << "Second class name = " << strName << endl;}
///\briefShowallclassnamesfromglobaldatavoid CSecond::ShowData(){ cout << "Access from class CSecond" << endl;// Get main class from global data CMain *pMain = m_pData->GetMain();// Get main class nameconstchar* strName = pMain->GetName(); cout << "Main class name = " << strName << endl;// Get first class from global data CFirst *pFirst = m_pData->GetFirst();// Get first class name strName = pFirst->GetName(); cout << "First class name = " << strName << endl;}