Where should global variables be placed ?

V

vivekian

Have a couple of global variables of a class type which i have declared
and defined in the application.

1. Not sure where should the global variables be placed ?
2. Would i need to forward declare the class before the global variable
is declared ?

Thanks,
vivekian
 
P

Phlip

vivekian said:
Have a couple of global variables of a class type which i have declared
and defined in the application.

1. Not sure where should the global variables be placed ?

In the .cpp file of their class type.
2. Would i need to forward declare the class before the global variable
is declared ?

Include the .h file of their class type at the top of that module.

Then, before everyone here starts yelling about "global" variables, make
them static variables of their class type, and see about making them
private.
 
M

Moonlit

Hi,

Well apart from the fact that you should try to avoid global variables
(sometimes they do come in handy however). I usually put them in the module
containing main or winmain.
Since I only use globals for things that really should be global (like a
class to log error messages) I use "extern" in the header file of the global
class and declare it in the main module. For instance for my logging class
(usually the only global I use).

-> Log.h

class CLog
{
....
};

extern CLog Log;


main.c

CLog Log( "/tmp/program.log" );

int main()
{
}

But this is just one way of doing it ofcourse.
--


Regards, Ron AF Greve

http://moonlit.xs4all.nl
 
V

vivekian

Well apart from the fact that you should try to avoid global variables
(sometimes they do come in handy however). I usually put them in the module
containing main or winmain.

Writing a small simulator which schedules tasks. In this case the
pointer to the current running task descriptor is the global variable
and should be visible across files. So as mentioned above i would have
something like

class task
{
....
} ;

extern task * current ;

Is making it global a good choice ? or is static fine ( have my doubts
though since the variable is too be present across files ?

thanks,
vivekian
 
V

vivekian

I would not use a global for this. I would add a "context" object that
is shared by other objects for this kind of thing.

Not sure what a "context" object is :)
 
R

roberts.noah

vivekian said:
Writing a small simulator which schedules tasks. In this case the
pointer to the current running task descriptor is the global variable
and should be visible across files. So as mentioned above i would have
something like

class task
{
...
} ;

extern task * current ;

Is making it global a good choice ? or is static fine ( have my doubts
though since the variable is too be present across files ?

A better design might be:

class task
{
private:
task * current;
public:
static task& current_task() { return *current; } // there better be
one too...
};

Now, you could make that code more robust by making sure there IS a
current task but the point is that no global is required and the class
itself keeps track of the things it should be tracking. One major
benefit is that you can place the checks to see if there is a current
task in ONE place instead of all over the damn place (any time you
access the global pointer you would need to check).

You could also have some sort of "task manager" class that maybe
contains all the tasks to be performed and manages them in some
way...it might be a better place to track the _current_ task. Your
task manager could very well be a singleton.
 
G

Gianni Mariani

vivekian said:
Have a couple of global variables of a class type which i have declared
and defined in the application.

Globals are Evil(TM). Okay okay, if they're read-only then they're not evil.

If you MUST.
1. Not sure where should the global variables be placed ?

For NON TEMPLATE variables. - Declared in header files, defined in non
headers.

For templates, you can define them in header files as well.
2. Would i need to forward declare the class before the global variable
is declared ?

You don't NEED to if you don't access it other than in the compilation
unit you use it. If you don't put them in a header file, it's also good
idea to place them in an anonymous namespace to avoid conflicts with
other compilation units.
 
G

Gianni Mariani

vivekian said:
extern task * current ;

Is making it global a good choice ? or is static fine ( have my doubts
though since the variable is too be present across files ?

I would not use a global for this. I would add a "context" object that
is shared by other objects for this kind of thing.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top