Vytvoříme čtyři třídy. CMain, CData, CFirst,CSecond.
Ve třídě CData budou uloženy ukazatele na ostatní třídy. Přes tuto třídu budeme přistupovat z jakékoli třídy k ostatním třídám.
#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; }};
Třída CFirst a CSecond obsahuje ukazatel na globální 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; }};
V konstruktoru uložíme ukazatel obou tříd do globálních dat 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);}
Hlavní třída CMain obsahuje ukazatele na ostatní třídy 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; }};
V konstruktoru třídy CMain alokujeme globální data m_pData = new CData();
Uložíme ukazatel třídy do globálních dat m_pData->SetMain(this);
Alokujeme ostatní třídy a předáváme jim ukazatel na globální 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);}
Funkce ShowData()
Získá u každě třídy ukazatel na ostatní třídy a vypíše jejich jména
///\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;}