English

g++

Příklad přístupu k datům ze všech tříd c++ aplikace (Data centered system)


07.03.2007

Download zdrojáku (68.3kB)

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>
using namespace 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 class
    public:
        CData();
        ~CData();

		///\brief Set pointer to main class
		void SetMain(CMain *_pMain)
		{
			m_pMain = _pMain;
		}
		
		///\brief Get pointer to main class
		CMain *GetMain()
		{
			return m_pMain;
		}

		///\brief Set pointer to first class
		void SetFirst(CFirst *_pFirst)
		{
			m_pFirst = _pFirst;
		}
		
		///\brief Get pointer to first class
		CFirst *GetFirst()
		{
			return m_pFirst;
		}

		///\brief Set pointer to second class
		void SetSecond(CSecond *_pSecond)
		{
			m_pSecond = _pSecond;
		}
		
		///\brief Get pointer to second class
		CSecond *GetSecond()
		{
			return m_pSecond;
		}
};

Třída CFirst a CSecond obsahuje ukazatel na globální data CData *m_pData;.

#pragma once

#include <iostream>
using namespace std;
#include "CData.h"
#include "CMain.h"


class CFirst
{
		const char* m_strName;	///< Class name
		CData *m_pData;			///< Global application data
    public:
        CFirst(CData *_pData);
        ~CFirst();

		void ShowData();

		///\brief Get first class name
		///\retval m_strName Class name
		const char *GetName()
		{
			return m_strName;
		}
};

#pragma once

#include <iostream>
using namespace std;
#include "CData.h"
#include "CMain.h"


class CSecond
{
		const char* m_strName;	///< Class name
		CData *m_pData;			///< Global application data
    public:
        CSecond(CData *_pData);
        ~CSecond();

		void ShowData();

		///\brief Get second class name
		///\retval m_strName Class name
		const char *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>
using namespace std;
#include "CFirst.h"
#include "CSecond.h"

class CMain
{
		const char* 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 class
    public:
        CMain();
        ~CMain();

		void ShowData();

		///\brief Get main class name
		///\retval m_strName Class name
		const char *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

///\brief Show all names of all classes
void CMain::ShowData()
{
	// Access to all classes from first class
	m_pFirst->ShowData();
	// Access to all classes from second class
	m_pSecond->ShowData();
}

///\brief Show all class names from global data
void CFirst::ShowData()
{
	cout << "Access from class CFirst" << endl;
	// Get main class from global data
	CMain *pMain = m_pData->GetMain();
	// Get main class name
	const char* 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;
}

///\brief Show all class names from global data
void CSecond::ShowData()
{
	cout << "Access from class CSecond" << endl;
	// Get main class from global data
	CMain *pMain = m_pData->GetMain();
	// Get main class name
	const char* 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;
}




home / g++


Valid XHTML 1.0 Transitional