   question about OOP
  i am working on a multiplayer game project, I have a design question about OOP I have a base class called Game also another class for networks called NetworksNetworks is a child of Game. I want to reach contents of Game from Networks but I think it is impossible in c++ (I could just send a refference in java but it does not work here.am I doing something wrong or is it really impossible? I can pass pointers to all necessary data from game to networks but it would be too hard I think.do you have any suggestion?[Edited by - jpetrie on September 29, 2009 7:39:15 PM]
  Can you explain what you mean when you say that Network is a "child" of Game? In OO, "child" usually means "derived class". Do you mean member?Anyways, the theory is the same regardless of language. In Java, you pass an object reference. In C++, you can pass a C++ reference as a parameter, or a (smart) pointer if you intend to store the object reference.   ;Quote:Original post by rip-offCan you explain what you mean when you say that Network is a "child" of Game? In OO, "child" usually means "derived class". Do you mean member?Anyways, the theory is the same regardless of language. In Java, you pass an object reference. In C++, you can pass a C++ reference as a parameter, or a (smart) pointer if you intend to store the object reference.ups, sorry. I tried to mean Game contains an object of NetworksBut I can't do that, am I doing some mistake. I can't include Game.h in Networks.hEDIT: WOW I AM STUPUD I USED "CHILD" again[Edited by - shultays on September 29, 2009 5:10:59 PM];  here is the structure of my project//Game.h#ifndef GAME_H#define GAME_H#include "Network.h"...class Game{  Networks net;  ...};#endif//Networks.h#ifndef NETWORKS_H#define NETWORKS_H//#include "Game.h" // gives errorclass Networks{  //Game *g;  //this is what I want};#endifbut I can't do that, I can't even include Game.h to Networks.h just including Game.h should compile it right? since GAME_H is now defined it actually won't include anything but it still gives errors   ;  EDIT: nope, it does not work :/ sometimes I can compile it sometimes I can't. it depends on which file I am editting (I am working on VS)   ;Quote:Original post by shultayshere is the structure of my project*** Source Snippet Removed ***but I can't do that, I can't even include Game.h to Networks.h just including Game.h should compile it right? since GAME_H is now defined it actually won't include anything but it still gives errorsNo.  The problem is that your Networks.h needs to know about your Game class, and your Game.h needs to know about your Networks class.  If you include Game.h in Network.h and Network.h in Game.h, then either:1) Network.h will not get the definition for your Game class from Game.h, due to your include guards2) Game.h will not get the definition for your Network class from Network.h, due to your include guards3) You will get a recursive include (assuming you take out the include guards)The solution to this problem is to forward-declare one of your classes.  For example, in Network.h, you would take out the #include "Game.h", then place "class Game;" before your Network class definition.  This is essentially telling Network that you have a class named Game that you will define later.   ;Quote:Original post by RycrossQuote:Original post by shultayshere is the structure of my project*** Source Snippet Removed ***but I can't do that, I can't even include Game.h to Networks.h just including Game.h should compile it right? since GAME_H is now defined it actually won't include anything but it still gives errorsNo.  The problem is that your Networks.h needs to know about your Game class, and your Game.h needs to know about your Networks class.  If you include Game.h in Network.h and Network.h in Game.h, then either:1) Network.h will not get the definition for your Game class from Game.h, due to your include guards2) Game.h will not get the definition for your Network class from Network.h, due to your include guards3) You will get a recursive include (assuming you take out the include guards)The solution to this problem is to forward-declare one of your classes.  For example, in Network.h, you would take out the #include "Game.h", then place "class Game;" before your Network class definition.  This is essentially telling Network that you have a class named Game that you will define later.thank you. I tried now it works but I can't access any member of Game (they are not defined)if I add #include "Game.h" to Network.cpp it works, but is that a good idea?or if I change my Game.h to something like#ifndef GAME#define GAME//all of the includes defines etc#endif GAMEclass Game{...};   ;Quote:thank you. I tried now it works but I can't access any member of Game (they are not defined)if I add #include "Game.h" to Network.cpp it works, but is that a good idea?Yes, that is what you want to do. Having a forward declaration of the referenced class in a header file followed by including the referenced classes header in the associated source file is a very good idea, it helps speed up compilation by removing the amount of headers included.Where reasonable, you should try avoid #include from headers.Quote:or if I change my Game.h to something like#ifndef GAME#define GAME//all of the includes defines etc#endif GAMEclass Game{...};Don't do this. It defeats the purpose of include guards, without solving the problem.   ;  ok then, I will just include Game.h in Networks.cpp.thanks you for your help   ;  Why does your Networks class need to know about your Game class though? I do not know how exactly you're 'designing' your game, but if my assumption that your Networks class only handles everything network-related is right, then, in theory, it has no need to know of the existence of your Game class.I think it would pay off for you to take a look at your class design, trying to get rid of any dependencies that Networks may have with your Game class.   