Jump to content
  • Advertisement
Sign in to follow this  
breakspirit

C++ How to get these classes to communicate?

This topic is 2613 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

Hey guys, I've got two classes, Game and NetCode and I'm trying to get them able to communicate. Here's some relevant code:
[heading]Main.cpp[/heading]

#include "Game.h"
#include "NetCode.h"

NetCode netConnection;
Game game;

main()
{
game.gameloop();
} etc...


[heading]Game.h[/heading]

class Game{
//DECLARATION OF NETCODE OBJECT?
//more stuff
}


[heading]Game.cpp[/heading]

#include "Game.h"

Game::gameloop()
{
here's the main game loop{
//need to be able to call NetCode methods here
}
}


[heading]NetCode.h[/heading]

extern Game game;


class NetCode
{
//stuff
}


[heading]NetCode.cpp[/heading]


#include "Game.h"
#include "NetCode.h"

//the rest of the NetCode methods.
//these need to be able to call Game methods, as shown
NetCode::examplefunc()
{
game.outputText("hey here's some output to parse");
}


The code I've got compiles, but I now need a way for Game methods to be able to call NetCode methods and vice versa. I tried having a NetCode object as one of Game's member variables, but I couldn't see a way to get NetCode to then call Game's methods. I also have tried giving Game a pointer to a NetCode object as a member variable but have been unable to make that work. Can anyone enlighten me as to the correct way to accomplish this that is in line with OOP principles?

Share this post


Link to post
Share on other sites
Advertisement
Forward declaration (also, look towards the end of this page (and in case you're really curious, here's more)). Forward declare the classes in the header files, and then include the appropriate header file in the source file to actually use the methods and members of the needed class.

i.e. in Game.h, just do a forward declaration of "class NetCode" and in NetCode.h do a forward declaration of "class Game". Only include the header files in the source files. And bam, you should be good.

Share this post


Link to post
Share on other sites

Forward declaration (also, look towards the end of this page (and in case you're really curious, here's more)). Forward declare the classes in the header files, and then include the appropriate header file in the source file to actually use the methods and members of the needed class.

i.e. in Game.h, just do a forward declaration of "class NetCode" and in NetCode.h do a forward declaration of "class Game". Only include the header files in the source files. And bam, you should be good.


I did that and added
extern NetCode netConnection;
to Game.h and that let me do
netConnection.packetLoop();
in Game.cpp, so it appears to have worked. It mysteriously crashes when a client connects to the server, but that may not have anything to do with this. Thanks a lot for the help!
Edit: the crash was completely unrelated. Whew. Thanks again.

Share this post


Link to post
Share on other sites
Why do the two objects need to communicate with each other in two directions?
This usually indicates something wrong in design.

If the two objects (A and B) share same knowledge, the same part should be a third object (C).

Then A, B call C for shared functions.
If A, B want to notify changes, just notify to C, then C dispatch to whoever cared. That's to say, A, B can subscribe to C for any other objects' changing event.

However, if you can't introduce C, then A and B have to hold the counterpart's forwarded pointer or reference.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!