Sign in to follow this  

C++ Inter-Class Communications

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've been strugling with this lately and I'm not sure how to go by it. The project I'm working on got into such a mess I finally decide it was time to start documenting it so I could design it on paper and them impliment it to make things easier. Well I want to have a single base class that will store all the 'global' data the other classes will need such as vectors of structs I have that contain specific data for files I've read from. What I need to be able to do is pass the data to another class for processing when it requests it in the code, but my problem is I don't know how. One of my programming friends suggested having getters that return a reference to the struct or vector objects. This is fine and dandy, but I have some structs that are used as the vector's datatypes, and I need to be able to reinterpret that data into another struct object in another class for easy processing. The alternative, I suppose, is to have setters and getters for each individual element of the struct objects in the base class, sounds tedious. What I'm asking is, maybe someone could point me to some good information on the subject, or maybe even try to explain it w/ an example? Thanks!

Share this post


Link to post
Share on other sites
very confused by your post, you are using some terms in ways that are a little mixed up ... please post a little bit of the code you currently have or or thinking about, and little very specifc english describing what you want to acomplish (show us some classes / code and tell use what you want it to DO ... not how to do it, just what the end goal is).

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
It sounds as if you are not using Object Orientation for your classes in a very clean way. (And/or your base code is pretty much plain C.) If so, I suggest seriously studying true OO until quasi-mastery is attained before proceeding - your end job will probably be made MUCH easier. (And will also probably be much cleaner, easier to maintain, and time to completion in a decent state will probably be lessened, as well. If you don't OOify it, your time to completion may be shorter, but your end product will be more brittle, and harder to maintain. If you don't forsee maintaining it, leave it as it is and crunch through this hurdle if you want the headache.)

David

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Sorry about double post. It seemed to hang, then responded that the script had timed out when I tried to send the first one, so I resent it without checking. Feel free to delete the duplicate and this one.

David

Share this post


Link to post
Share on other sites
Alright, maybe I wasn't to clear on this. My goal is to have multiple classes get data from a single class, much like clients to a database server. This single class is going to hold my vectors which store different data types like string and such, but the twist is that I have vectors of my own structs.

That doesn't sound bad you say, just make objects of the structs or make vectors of that datatype inside that class. Ok, that's fine, but I need to be able to do that inside of other classes too. Maybe I'm not doing this right, there might be an easier way, but I need to be able to call a struct object of data from inside this single class, and assign it to a struct object in another class for processing.

The problem is, besides using setters and getters, how am I going to contain the data in those other classes once I 'get' it from the single class, when I don't have access to those structs to make objects of them to contain the data?

Share this post


Link to post
Share on other sites
Wait, are you talking about what is essentially variant typing? You want a single datastore in which you want to store and retrieve different types of data?


class Variant; // Define your own variant or ask and I'll post some code

class Database
{
public:
Database( size_t p_size )
{
m_data.reserve( p_size );
}

Variant GetData( size_t p_offset )
{
assert( p_offset >=0 && p_offset < m_data.size() );
return m_data[ p_offset ];
}

void SetData( size_t p_offset, Variant p_data )
{
assert( p_offset >=0 && p_offset < m_data.size() );
m_data[ p_offset ] = p_data;
}

private:
std::vector< Variant > m_data;
};

class MyDataConsumer
{
public:
MyDataConsumer( Database &p_db ) : m_db(p_db) { }

void Test()
{
m_db.SetData( 5, Variant("Hello world" ) );
std::cout << m_db.GetData( 5 ) << std::endl;
}

private:
Database &m_db; // Database to use for info
}

int main()
{
Database db(10);

MyDataConsumer cons( db );
cons.Test();

return 0;
}



At least, something like that if I understand your question correctly.

Share this post


Link to post
Share on other sites
Can't say I know what that is. :(

Here, let me try to whip up something:


class CBase
{
private:
CBase();

//structs
struct sHeader{ int magic; int x; int y; }; //Map's header
struct sParam{ int weather; int weather_delay; }; //Map's parameters
struct sMap{ int tile_ID; int tile_ID2; bool clip; int sprite; int entity; }; //Map's data
struct sFrame{ SDL_Surface image; bool end; }; //Texture or frame of animations sequence
struct sTxtData{ int x; int y; int x_cut; int y_cut; int delay; int trans; int alpha; }; //Textute data (.ntf)

//Data vectors
vector<sMap> vecMapData; //Map Data
vector<string> vecTxtFilenames; //Texture Filenames
vector<sFrame> vecTextures; //Textures
vector<sTxtData> vecTxtData; //Texture Data
vector<SDL_Surface> vecSprites; //Sprites

//Struct objects
sHeader Header;
sParam Param;
sMap *Map;
sFrame *Frame;
sTxtData TxtData;

//Vars
int Screen_X;
int Screen_Y;

public:
int getScreenX(){ return Screen_X; };
int getScreenY(){ return Screen_Y; };
void setScreen(int x, int y){ Screen_X = x; Screen_Y = y; };
};



That's just the base class I'm in the middle of working on. See how I have those structs, vectors, and struct objects in there? I want to share all that data throughout in the other classes, but use proper data hiding techniques (do I need them here for this?). Does that help anymore? :(

Share this post


Link to post
Share on other sites
Ahhh I see now.

Ok, take the struct declarations out of the class, or at least make them public. Then declare some public methods for obtaining references to the items you want:

eg:


class CBase
{
public:
struct sHeader{ int magic; int x; int y; }; //Map's header
struct sParam{ int weather; int weather_delay; }; //Map's parameters
struct sMap{ int tile_ID; int tile_ID2; bool clip; int sprite; int entity; }; //Map's data
struct sFrame{ SDL_Surface image; bool end; }; //Texture or frame of animations sequence
struct sTxtData{ int x; int y; int x_cut; int y_cut; int delay; int trans; int alpha; }; //Textute data (.ntf)

int getScreenX(){ return Screen_X; };
int getScreenY(){ return Screen_Y; };
void setScreen(int x, int y){ Screen_X = x; Screen_Y = y; };

typedef vector<sMap> tMapVector;
typedef vector<string> tFilenameVector;
typedef vector<sFrame> tTextureVector;
typedef vector<sTxtData> tTextureDataVector;
typedef vector<SDL_Surface> tSpriteVector;

sHeader &getMapHeader() const { return Header; }
sParam &getParam() const { return Param; }
sMap &getMap() const { return Map; }

tMapVector &getMapData() const { return vecMapData; }
tFilenameVector &getFilenames() const { return vecTxtFilenames; }
tTextureVector &getTextures() const { return vecTextures; }
tTextureDataVector &getTextureData() const { return vecTxtData; }
tSpriteVector &getSprite() const { return vecSprites; }


private:
CBase();

//Data vectors
tMapVector vecMapData; //Map Data
tFilenameVector vecTxtFilenames; //Texture Filenames
tTextureVector vecTextures; //Textures
tTextureDataVector vecTxtData; //Texture Data
tSpriteVector vecSprites; //Sprites

//Struct objects
sHeader Header;
sParam Param;
sMap *Map;
sFrame *Frame;
sTxtData TxtData;

//Vars
int Screen_X;
int Screen_Y;

};





edit: I noticed you need to hide things, what do you need to hide and how?

Also, do you have some examples on how you'll be accessing this data?

Share this post


Link to post
Share on other sites
Hmm, that makes sense. I don't know why I didn't think of that on my own. =/

I don't need to typedef the vectors though, those are the objects I want to store the data in. Any reason to do so besides making more objects?

I don't have any specific example right now besides the logic of it. I have a CLoad class which reads data out of a file and into those vector objects in that CBase class. That's how it will work for the other ones too. Then the other way around is I need to access that data from another class, such as my CSprite class which is going to handle loading the sprites from file after it reads the filename vector to get them. I'm using SDL just FYI.

Share this post


Link to post
Share on other sites
Quote:
Original post by chbrules
I don't need to typedef the vectors though, those are the objects I want to store the data in. Any reason to do so besides making more objects?


I always typedef things like this, if not because it makes it easier to change later and keeps things consistent outside the class. I also normally typedef my iterator.

I hope you manage to get this working how you need to, post again if you get stuck, I'm sure someone will be able to help you :)

Share this post


Link to post
Share on other sites
Thanks a ton! One last question comes to mind. Say I wanted to make a struct object of one of those structs in another class, do I just do:


CBase::sFrame *Frame;


?

Share this post


Link to post
Share on other sites

This topic is 4665 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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