Czech

g++

Data access c++ example (Data centered system)


07.03.07

Download source (68.3kB)

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>
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;
		}
};

Class CFirst and CSecond contains pointer to global 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;
		}
};

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>
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;
		}
};

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.

///\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