I hope I understood it correctly. You want to use the Gameinfo in another class? Probably you know 99% of the stuff I will write already, but I don't know which percent you are missing - so sorry if this will get long :-)
I think you are mixing up classes and instances (objects) somewhat. Gameinfo is a class definition describing how objects of those type will look like. But once you create real objects (which means really nothing but: reserve a block of memory which is large enough to contain all variables mentioned in a class) and you want to use that data in some other objects you have to pass it to that other object. Doesn't matter if that other object is of the same class or derived from that class or of another class type. You always will have to pass it if you want to access that data.
So to use that data from your variable 'game' you will have to pass it to objects of those classes which want to access it. This means: Add a function to the class which receives the data. You even need to add that function when passing objects of the same class (often that function is the so called copy-constructor). You have several ways to do that, by object, by reference or by pointer . You could call that function "setGameinfo". Or often you will just add a parameter in the constructor and use that to pass the object. Within that function (or constructor) you have now once more the choice what to do. Sometimes you will directly access the data, but here you will probably want to copy the data or keep a pointer or reference to it so you can access it later on. That copy is another variable of the receiving class. Usually you use references or pointers (internally they are the same), because that will save memory and is faster.
As example (using the constructor to pass the data as reference and saving it in the class also as a reference):
Code: Select all
class NCamera
{
Gameinfo & myReferencetoAGameObject; // reference
NCamera(Gameinfo &refToAGameInfoObject) : myReferencetoAGameObject(refToAGameInfoObject) {}
};
And now you can pass your game to any object of NCamera. But you must be careful. The reference will point to that memory which you allocated when you did push your 'game' variable on the stack. So when you created 'game' locally it will be invalid as soon as that local scope is ended. Scope is always from { to }. No problem if you create it within main() - because when main is ended your application is ended. But a big problem if you create 'game' for example in a subfunction.
Just to make the difference between class/object more clear. What would have happened on inheritance? Like your example with "class NCamera : public Gameinfo"?
A class describes just how object will look like in memory. So in this case whenever you create a NCamera object it will not only reserve memoryblocks large enough to contain all variables in NCamera but it will reserve a block large enough to contain memory for all variables in NCamera and in Gameinfo. And you could addionally access all that memory in such objects using the variablenames of NCamera and Gameinfo (except private names of Gameinfo - though you could still access even that memory quite easy with simple tricks). But you could not access any memory allocated for other objects (not even of same type). And the reason for that is that you don't know where those other objects are. Not before you pass that information to the class.
It's very important to understand the way data is hanging around in memory. All that stuff with classes and variables is just there because it helps to avoid a lot of bugs when you do no longer need to access the data using numbers.
edit: Another example. This time we pass a pointer and keep a real copy in the camera. While this is not recommended I wanted to add that because of the differences. Both examples will work (unless I made some typos), but they do different things.
Code: Select all
class NCamera
{
NCamera() {} // just to show I do nothing here
Gameinfo myGameObject; // real copy
void setGameinfo( Gameinfo * info) { myGameObject = *info; }
};
There are several things to note here:
- myGameObject will contain random data, but it will call the constructor of Gameinfo (so it's important to always set all variables to non-random values in the constructor!!!)
- Because a real copy is made you don't have to care if the memory of your Gameinfo object which you pass to NCamera points afterwards still to valid memory. Scopes no longer matter.
- It will use more memory now.
- Gameinfo might still contain data which can get invalid at some time. Even a real copy might cause problems if for example pointers which are within Gameinfo are suddenly invalid (for example because the device was destroyed).