Archived

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

C++, what is the correct way to do this?

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

Hi, I have a program made with many classes in C++. In one of these classes, there is a variable. I would like to make this variable accessible from anywhere in my programs.. ( and accessible within all my classes ) How would it be possible to do that? I could create a pointer and make it point to my variable, in each of my calsses.. but it sucks to create a lot of pointers just for a single variable.. What could be the best way to do this? Thanks

Share this post


Link to post
Share on other sites
Sounds like you need a singleton. I typed this example code from a book I'm reading, Core Techniques and Algorithms in Game Programming


Here is the code for a sample singleton:

class Singleton {
public:
static Singleton* Instance();
protected:
Singleton();
private:
static Singleton* _instance;
};

Singleton* Singleton::_instance = 0;

Singleton* Singleton::Instance () {
if (_instance == 0)
{
instance = new Singleton;
}
return _instance;
}



Any class that needs access to the Singleton creates an instance pointer.

[edited by - yspotua on February 21, 2004 8:24:32 PM]

Share this post


Link to post
Share on other sites
I have another very evil, insidious, posibly illegal in some states, hack-trick. So evil, i''m afraid to share it. I make no guarantees. But it''s works wonders for my project

i have a class called PointerHub. It has a list of static pointers to game objects of which there are only one (the map, the rendering engine, the network engine, etc).


class PointerHub {
public:
static Map* map;
static GraphicEngine* ge;
static Network* net;
}


Then whenever you have a class that needs one or more of these things, just inherit from from it!


class SomeClass: public PointerHub {
// whatever
}


Then the class can use all of those "psuedo-globals" by name whenever it darn well feels like.

As a natural consequence, the actual objects that PointerHub has it''s pointers pointing to need to "register" themselves in their constructors, like:


class GraphicsEngine: public PointerHub {
GraphicsEngine::GraphicsEngine() {
// register myself in the pointerhub
ge = this;
}
}


i told you it was evil. beeeeeware....


Share this post


Link to post
Share on other sites
Thanks for the replies.
I''ll look at your solutions, but first, I thought about something.. Maybe my code is badly designed?

Here is what I want to do:
I have a variable stored in my ''EngineSystem'' class. This variable is called m_timeFrame.
It hold the time spent between 2 frames..
When I have something that is movement related, I want to multiply this variable (m_timeFrame) with the speed of the object moving.

Exemple, I have a class ''Player'' and this class has the fonction move.
void Player::Move()
{
m_Pos += m_Speed*m_timeFrame;
}


My problem is that m_timeFrame is a variable from the class ''EngineSystem'' and is not accessible in the ''Player'' class..

Is my design wrong?
What should I do in this situation?

Share this post


Link to post
Share on other sites
quote:
Original post by Hedos
Exemple, I have a class 'Player' and this class has the fonction move.
void Player::Move()
{
m_Pos += m_Speed*m_timeFrame;
}

My problem is that m_timeFrame is a variable from the class 'EngineSystem' and is not accessible in the 'Player' class..
Is my design wrong?
What should I do in this situation?


The alternatives to a singleton-pattern are: a) passing the values through a lot function-calls or b) keeping a pointer to this variable in a lot of objects.
b) is clearly worse than a singleton, higher overhead, nothing gained
a) is slower and more verbose but shows dependencies more clearly and might be easier to test/debug

[edited by - Trap on February 21, 2004 9:48:40 PM]

Share this post


Link to post
Share on other sites
"a) passing the values through a lot function-calls"

Does you mean passing the value as an argument in the fonction?
Exemple:
Player.Move( m_timeFrame );

That doesn''t look like a really good way, always passing the -same- argument to a fonction.

So would you suggest to use singleton?
It looks like a complicated solution

Or method a) would be better?

Share this post


Link to post
Share on other sites
it depends how your system is arranged, i'd be more inclind to pass the time into the function if i could than pull it from somewhere else, at worse at the top of your main loop you could pull it into the update function and then pass it along to all the object you call (not just players, but anything else that requires time based processing), it would make it a bit easier to track program flow imo

they would also all get the same time as well

Basic idea;

function Update()
{
int currenttime = EngineSystem::Instance().getTime();
for(int i = 0; i < number_of_things_to_update; ++i
{
object[i].Move(currenttime);
}
}


[edited by - _the_phantom_ on February 21, 2004 11:32:57 PM]

Share this post


Link to post
Share on other sites
The Phantom's way makes sense to me; that's how I'm doing it. Really, what does it matter if you're passing a number into some functions every frame? It can't have more processing demands than all those vector objects you have. Am I right?


leiavoia: That's what I'm doing! Haven't tested it yet, but boy, you better believe I was really pleased with myself when I thought of that: I thought: Eric, you are a genius! But now I have just gotten to the point where I needed to initialize the variables with new, and was wondering where to do that. Now that I've seen your code, I've got something to try. Thanks! Can't wait to see this evil in action!

[edited by - EGD Eric on March 29, 2004 10:43:41 PM]

Edit: Ok, this is just weird. The compiler actually lets me do this:


//PointerHub.cpp

TextureManager PointerHub::TextureManager = new TextureManager;


I just assumed this was illegal. Crazy!

[edited by - EGD Eric on March 29, 2004 11:07:59 PM]

Share this post


Link to post
Share on other sites