Archived

This topic is now archived and is closed to further replies.

TechnoGoth

Two way Comunication between classes

Recommended Posts

TechnoGoth    2937
Hello all, I have a probelem, I have two classes Game Engine and Game Layer. Game engine handles all the input and output with the user. While Game layer handles all the game specific code. My problem is I want them to comunicate with each other. But I get a compiler error when ever I try to compile them since they each have a pointer to the other. Is there away to get this working? ----------------------------------------------------- Writer, Programer, Cook, I''m a Jack of all Trades Current Design project Chaos Factor Design Document

Share this post


Link to post
Share on other sites
Mastaba    761
The solution is to use forward declarations. Also as a sidenote, I personally wouldn''t allow the engine to access any members of the game class. Otherwise, your engine class is likely going to be not very reusable.

Share this post


Link to post
Share on other sites
TechnoGoth    2937
I''m affraid I don''t know what forward declarations are. Also neither class can directly memebers of each other, They just have access to varius public methods.

-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document

Share this post


Link to post
Share on other sites
MaulingMonkey    1730
Hopefully you have the classes seperated into decleration (in a header) and function code (in a source file). If so, it's really simple. An example:


classa.hpp
#ifndef CLASS_A
#define CLASS_A
class B; //forward decleration of class B

class A
{
public: //for lazy example
B *otherclass;
public:
void whatever( void );
void confirm_whatever( void );
};
#endif //ndef CLASS_A


classb.hpp
#ifndef CLASS_B
#define CLASS_B
class A; //forward decleration of class A

class B
{
public: //for lazy example
A *otherclass;
public:
void do_whatever( void );
};
#endif //ndef CLASS_B

classa.cpp
#include "classa.hpp"
#include "classb.hpp"
#include <iostream>

A::whatever( void )
{
otherclass->do_whatever();
}
A::confirm_whatever( void )
{
std::cout << "Whatever confirmed!!!" << std::endl;
}

classb.cpp
#include "classa.hpp"
#include "classb.hpp"

B::do_whatever( void )
{
otherclass->confirm_whatever();
}

main.cpp
#include "classa.hpp"
#include "classb.hpp"

int main ( int argc , char ** argv )
{
A a;
B b;
A.otherclass = &b;
B.otherclass = &a;
A.whatever();
//preformed actions:
//A.whatever() calls: B->do_whatever()
//B->do_whatever() calls: A->confirm_whatever()
//A->confirm_whatever() prints confirmation
return 0;
}


edit: decided to be nice and make a fully working example

[edited by - MaulingMonkey on August 10, 2003 4:57:44 AM]

Share this post


Link to post
Share on other sites
bzroom    647
why don''t u just have ur game layer update variables in teh game engine layer or like he said its not going to be very reusable. Also it would help if u posted some code but its ilke this





//gameengine.h


class gamelayer;


class gameengine
{
public:
gamelayer *Dgamelayer;

gameengine();
setup(gamelayer *Pgamelayer){Dgamelayer=Pgamelayer;}
};



//gamelayer.h

class gameengine;

class gamelayer
{
public:
gameengine*Dgameengine;


gamelayer(gameengine*Pgameenginer){Dgameengine=Pgameengine;}
};

int main
{
gameengine *ge = new gameengine();
gamelayer *gl = new gamelayer(ge);

gameengine->setup(gl);

//.......go on

}







Share this post


Link to post
Share on other sites
TechnoGoth    2937
Thanks, for the help the forward declaration seems to be working fine.

Also, The GameEngine as it is so far has no game specific code.


MapFile * getMapTile(int mX,int mY) {return &map[mX][mY];}
void interactionEffect(int interIndex, int x,int y);


At present these are the only methods in GameLayer that Game Engine can call, and all it needs to call.

-----------------------------------------------------
Writer, Programer, Cook, I''m a Jack of all Trades
Current Design project
Chaos Factor Design Document

Share this post


Link to post
Share on other sites