Sign in to follow this  
richy486

question about pointers, classes and communication between classes

Recommended Posts

When structuring an OO program what is the best/usual procedure for classes to communicate to each other. I had a system where each class had a pointer to any class it needed to communicate too, but this got rather messy. I was thinking of a system where one class holds pointers to all the classes and each class calls this pointer class to communicate with another class, but I am having some trouble implementing it. Is there a better way? thanks

Share this post


Link to post
Share on other sites
This is a really simple example but but it gets the idea out there.


class A {
private:
int m_nVarA;

public:
int GetVarA() { return m_nVarA; }
};

class B {
private:
int m_nVarB;

public:
int GetVarB() { return m_nVarB; }
};


By having get and send methods to your classes you can have classes talk to each other. This way B doesn't need a pointer to A, just call A.GetVarA() and B and get the value of m_nVarA.

Share this post


Link to post
Share on other sites
It isn't an actual multi-threaded kernel, with real processes but it is structered similar to the windows kernel. When you want to add a class to the kernel you derrive your class from a base class called 'process' and then add it. When then kernel calls Initialise on the process it passes a pointer to itself to it. So then the process can say pKernel->GetProcessByName() and it gets the pointer if one exists.

Hope that helps,

Dave

Share this post


Link to post
Share on other sites
pasing the pointer of it self to it, is what im having trouble withI'm probably just forgetting something but when i include the header file of the base class the new class is created from, i get errors such as:

: error C2146: syntax error : missing ';' before identifier 'm_Config'
: error C2501: 'CDataMan::CConfig' : missing storage-class or type specifiers

Where CDataMan is the base class and CConfig is the new class created in CDataMan refered to as m_Config.

Share this post


Link to post
Share on other sites
Here are the classes in question:

//-----------------------------
//---- CDataMan.h
#pragma once

#include ".\config.h"
#include ".\filereader.h"

class CDataMan
{
protected:
enum configData
{
WIDTH = 0,
HEIGHT
};

CConfig m_Config;
CFileReader m_FileReader;


public:
CDataMan(void);
~CDataMan(void);

bool initConfig();
int readConfig(int configDataType);

CConfig* getConfigPointer();
};

//---------------------------
//---- CDataMan.cpp
#include ".\dataman.h"

CDataMan::CDataMan(void)
{
}

CDataMan::~CDataMan(void)
{
}

bool CDataMan::initConfig()
{
return true;
}

int CDataMan::readConfig(int configDataType)
{
//testing junk:
if (configDataType == WIDTH)
return 0;
else if (configDataType == HEIGHT)
return HEIGHT;
}

//-------------- POINTERS ----------------------
CConfig* CDataMan::getConfigPointer()
{
return &m_Config;
}

//-----------------------
//---- CConfig.h
#pragma once
#include ".\dataman.h"

class CConfig
{
protected:

public:
CConfig(void);
~CConfig(void);

bool readConfigFile();
void setConfig(int configDataType, int configValue);
int getConfig(int configDataType);
};

//-----------------------
//-- CConfig.cpp
#include ".\config.h"

CConfig::CConfig(void)
{
}

CConfig::~CConfig(void)
{
}

bool CConfig::readConfigFile()
{
return true;
}

void CConfig::setConfig(int configDataType, int configValue)
{
}

int CConfig::getConfig(int configDataType)
{
return 0;
}

Share this post


Link to post
Share on other sites

Hi richy486,

I just took a good look at your code. I see peers doing this type of thing all the time. It's really implementation specific.

In CDataMan, you have an object called of type CConfig. You also have a function called CDataMan::getConfigPointer() which returns a pointer to a CConfig object. This works fine unless you don't want somebody else to change the contents of the CConfig object (there are ways around this).

There are other ways to do this. You could have methods in CDataMan that allow you to modify/retrieve contents from the CConfig object instead.

For example, you could implement a member function in CDataMan as follows:


int CDataMan::getConfig(int configDataType)
{
return m_Config.getConfig( configDataType );
}



You could even just use inheritance instead ( I try to use inheritance for code-reuse purposes whenever possible - many say it's a bad idea ) and would more than likely work in this situation, yet I don't know the relationships between your classes.

End of the day, there is no right or wrong, yet I find that member functions that return pointers are more than often clumsy (but that is my opinion), but sometimes pointers are ideal.

Hope that I was of some help, but maybe somebody more experienced could share some better insight into the topic?

Regards,

GCS584

Share this post


Link to post
Share on other sites
Quote:
There are other ways to do this. You could have methods in CDataMan that allow you to modify/retrieve contents from the CConfig object instead.

That's a great idea I think I'll use it. But if DataMan class had functions to CConfig and other classes involved with data management, and a Render class had functions to rendering the player and to rendering the gui, would it be a good idea for each of these base classes to have pointers to each other, I plan to have about 8 or 9.

Thanks for the help everybody too.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this