Sign in to follow this  
Revelation60

Accessing variables from another sibling

Recommended Posts

Hi, Although I have programmed C++ for quite a while, I never got into classes. But for my new game engine I decided it was best to make a giant wrapper for all low-level functions. The structure is like this:
class MYEngine
{
EngineWindowClass EngineWindow;
EngineTimingClass EngineTiming;
};

class EngineWindowClass
{
public:
int SomeVar;
};

class EngineTimingClass
{
public:
void SomeFunction(int foo);
};

What I want to do is access SomeVar in the SomeFunction function. Is this possible?

Share this post


Link to post
Share on other sites
Needless to say that SomeFunction() needs to be passed an instance of the class that holds the variable, aside from that the class holding the variable needs to be given accessors so that the function can get the variable.

You need to work out the relationship between the timing and window classes. If neither owns the other and since they are both owned by the MYEngine class, i would be tempted to give the timing and window classes a pointer back to MYEngine and have the interface for MYEngine provide access to the classes.

There are several ways you can do this and the above is only one option.

PS. I don't see the need to store it all in a MYEngine class, that's just a bit to hight level for my liking.

Share this post


Link to post
Share on other sites
Quote:
What I want to do is access SomeVar in the SomeFunction function. Is this possible?
No, not as you have things now.

First of all, I question the wisdom of cramming all of your modules into a single 'engine' class. What's the purpose of this exactly?

Now, to address the particular question of how to grant object B access to data in object A, I would start by asking yourself exactly why this access is needed (and perhaps explaining it to us as well :). Depending on the answer to this question, there are a few different directions you might go, such as:

1. Having module B hold a reference of some sort to module A.
2. Linking module B to module A via a delegate or callback.
3. Having modules A and B communicate via a messaging system.

[Edit: 4. Pass a reference to module A to the method in question, as suggested by Dave, above.]

Share this post


Link to post
Share on other sites
Well, my main idea was that I had one variable that is the entire engine and that the engine could easily be made into a game.
So:


MyEngine Engine;

Engine.EngineWindow.CreateWindow(...);
Engine.EngineWindow.DrawFunction = &AFunctionInTheGameItself;
Engine.EngineTiming.TimerFrequency = 100.0f;


The problem emerges when I need to access the TimerFrequency for the game loop in EngineWindow or when I need to access the hDC of a window, stored in EngineWindow, to create a font in EngineFont.

It's a pity something like parent.EngineWindow doensn't exist.

Share this post


Link to post
Share on other sites
Quote:
It's a pity something like parent.EngineWindow doensn't exist.


No... not really.

If you're running into such problems, you're getting design problem symptoms. Simply put, your EngineWindow and EngineTiming are simply unrelated, and you're hoping that cramming them into a single class will suddenly solve that.

Quote:
The problem emerges when I need to access the TimerFrequency for the game loop in EngineWindow or when I need to access the hDC of a window, stored in EngineWindow, to create a font in EngineFont.


Let's put this into code:

struct EngineWindow
{
EngineWindow( TimerFrequency * tf ); // we'll need a timer
};

struct EngineFont
{
EngineFont( EngineWindow * w ); // we'll need that to create font
};

...
void main()
{
TimerFrequency tf;
EngineWindow w(&tf);
EngineFont f(&w);
}



Look ma, no superclass, no heavy coupling.

Of course, going full RAII would remove the need to store the references between systems.

Share this post


Link to post
Share on other sites

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