PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

global variables

hi,how can i need a variable from one class in two others:
i create a bufferclass which has the functions "put" and "get" and so on....
i need "put" in one class and "get" in the other!!

1.) is it good to make this variable global (i do not think so) or
2.) how can i solve my problem.



maybe someone can give me a hint
It sounds like you're wanting to do something "nasty"/"un-OO", but maybe a public static variable on one of the classes is what you're after?

I may not have understood what exactly you want though - post some pseudo-code to explain what you want?
hi!
"put" and "get" are functions of my RingBuffer-Class.
So i want to call "put" from my Serial-Class to write to my ringbuffer and i want to call "get" from my Draw-Class to read from buffer.
If there's only one RingBuffer instance in operation, you may want to look at the Singleton design pattern. So, both your Serial-Class and Draw-Class have a pointer to your RingBuffer and both initialise it by calling the RingBuffer's (static) dummy constructor (which returns the RingBuffer's instance).

If not, a global variable (or public variable on a global class) is probably the way to go.
BurningSheep · 10 Mar 2003, 15:59 UTC
Singleton class sounds as the way to go here, I happen to have a really simple piece of code that implements this. It's a really basic wrapper class for FMOD, since there is only one sound engine I made it a singleton class.

http://vulcanus.its.tudelft.nl/robert/PPC/sound.zip

Check out the GetSound() function for the basic principle.
gery wrote:
"put" and "get" are functions of my RingBuffer-Class.
So i want to call "put" from my Serial-Class to write to my ringbuffer and i want to call "get" from my Draw-Class to read from buffer.


So whats the problem?

Ringbuffer
{
public:
const buffer * get();
void put(const buffer *);
...




But on another note, I wouldn't use Singleton to simulate globals anyway, use a namespace or static class. You introduce a lot of uselessness by wrapping globals in a class, actually any class which can't become many objects is a bit of an abomination. In other words Singleton ain't OO.
I am not a super crazy coder, but sometimes globals are ok. There are ways to make your code entirely devoid of them, but thats often alot of work. Im not sure what your doing exactly, but maybe you could use extern to have other classes edit the variable(if of course your not inheriting anything else)?
There is no (almost) difference between a global instance (variable) and a singleton. The only difference is that with a singleton you can control when it is initialized/destroyed. And I could argue that this is usually not a benefit.

I find that people are quick to say "singleton" to solve the "global instance" issues. This is simply wrong. For more info, I suggest you read the March 2003 issue of the C++ User Journal and look for the article by Herb Sutter. He explains very well why singletons are no better (and often worst) then global variables.

Note that I am not saying you should use global variables, they are (generally) a really bad idea.
Thierry wrote:
Note that I am not saying you should use global variables, they are (generally) a really bad idea.


Properly encapsulated, why would they be a bad idea?

I frequently, or perhaps always have a few globals. And my general technique is to put them in a namespace named Global. I [ab]use namespaces, espcially for constants which I wrap enums with.

This goes doubly with windows programming. I abhor MFC so I generally use the base Win32 API which almost forces one to have globals.

This is what I am speaking of:
Code:

namespace Global
{
enum
{
Width = 240,
Height = 320,
};

extern HINSTANCE instance;
};


Clean, efficient, stable memory use, what other benefits?
Sm!rk":1t4d5ozn]


By definition, a global is not encapsulated at all.


This is what I am speaking of:
Code:

namespace Global
{
enum
{
Width = 240,
Height = 320,
};

extern HINSTANCE instance;
};



If you are going to have globals, I agree that this is the way to go: with a namespace.

In your specific example, I have to wonder why you would want to hard code the width and height. In any case, constants would be much better:

Code:

namespace Constants
{
const int Width = 320;
const int Height = 240;
}
Thierry wrote:
In your specific example, I have to wonder why you would want to hard code the width and height. In any case, constants would be much better:

Code:

namespace Constants
{
const int Width = 320;
const int Height = 240;
}



Why do you say that? I'm almost sure optimization will boil either of those down to the same thing. The example with width and height was not typical, mostly its a hundred different item types, or spells or whatever and the autonumbering is useful.