C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Where should global variables be placed ?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
vivekian
Guest





PostPosted: Sun Nov 27, 2005 7:37 pm    Post subject: Where should global variables be placed ? Reply with quote



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

Back to top
Phlip
Guest





PostPosted: Sun Nov 27, 2005 7:42 pm    Post subject: Re: Where should global variables be placed ? Reply with quote



vivekian wrote:

Quote:
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.

Quote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!



Back to top
Moonlit
Guest





PostPosted: Sun Nov 27, 2005 7:45 pm    Post subject: Re: Where should global variables be placed ? Reply with quote



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

"vivekian" <viveklinux (AT) gmail (DOT) com> wrote

Quote:
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




Back to top
vivekian
Guest





PostPosted: Sun Nov 27, 2005 7:54 pm    Post subject: Re: Where should global variables be placed ? Reply with quote

Quote:
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


Back to top
vivekian
Guest





PostPosted: Sun Nov 27, 2005 8:46 pm    Post subject: Re: Where should global variables be placed ? Reply with quote

Quote:
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 :)


Back to top
roberts.noah@gmail.com
Guest





PostPosted: Sun Nov 27, 2005 8:47 pm    Post subject: Re: Where should global variables be placed ? Reply with quote


vivekian wrote:
Quote:
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 ?

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.


Back to top
Gianni Mariani
Guest





PostPosted: Sun Nov 27, 2005 8:55 pm    Post subject: Re: Where should global variables be placed ? Reply with quote

vivekian wrote:
Quote:
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.

Quote:

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.

Quote:
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.

Back to top
Gianni Mariani
Guest





PostPosted: Sun Nov 27, 2005 9:40 pm    Post subject: Re: Where should global variables be placed ? Reply with quote

vivekian wrote:

Quote:

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.

Back to top
Ian
Guest





PostPosted: Sun Nov 27, 2005 10:23 pm    Post subject: Re: Where should global variables be placed ? Reply with quote

[email]roberts.noah (AT) gmail (DOT) com[/email] wrote:
Quote:
vivekian wrote:

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 ?


A better design might be:

class task
{
private:
task * current;
You forgot 'static' here, static task* current;


Ian

Back to top
roberts.noah@gmail.com
Guest





PostPosted: Sun Nov 27, 2005 10:32 pm    Post subject: Re: Where should global variables be placed ? Reply with quote


Ian wrote:
Quote:
roberts.noah (AT) gmail (DOT) com wrote:
vivekian wrote:

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 ?


A better design might be:

class task
{
private:
task * current;
You forgot 'static' here, static task* current;

I sure did. Thank you.


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.