473,385 Members | 1,373 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Where should global variables be placed ?

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

Nov 27 '05 #1
9 2887
vivekian wrote:
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.

--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
Nov 27 '05 #2
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" <vi********@gmail.com> wrote in message
news:11**********************@o13g2000cwo.googlegr oups.com...
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

Nov 27 '05 #3
>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

Nov 27 '05 #4
vivekian wrote:
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.
Nov 27 '05 #5
vivekian wrote:

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.
Nov 27 '05 #6
>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 :)

Nov 27 '05 #7

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;
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.

Nov 27 '05 #8
Ian
ro**********@gmail.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;

Ian
Nov 27 '05 #9

Ian wrote:
ro**********@gmail.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.

Nov 27 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: Larry L | last post by:
I have a Module that declares several arrays as public, some string, some integers. I then have 2 forms. Form A is the main form, that loads on start-up, and has a command button to open Form B. On...
17
by: Jonas Rundberg | last post by:
Hi I just started with c++ and I'm a little bit confused where stuff go... Assume we have a class: class test { private: int arr; };
6
by: rick | last post by:
Noob problem. I prefer to keep all my scripts in an external '.js' file. I am currently loading the external '.js' file from the header. Problem is I would like to declare a global variable in the...
1
by: Robert North | last post by:
-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 I've just started programming JS, and one question keeps nagging me: I can insert a <script> element anywhere in HTML, and when it's executed...
5
by: Prabhat | last post by:
Hi All, My website is hosted with the ISP and recently I have modified my Global.asa file for the ASP application. So I think we need to restart the World Wide Web Publishing Service so that...
7
by: S. A. Hussain | last post by:
Where Global variables created in STACK or HEAP in C/C++? ve##tolimitsyahoocom, delete ##
11
by: Neo | last post by:
Where does global, static, local, register variables, free memory and C Program instructions get stored? -Neo
4
by: raylopez99 | last post by:
Why is the same variable local inside a 'foreach' loop yet 'global' in scope (or to the class) outside it? RL class MyClass { int MyMemberArray1; //member variables, arrays, that are...
2
by: kstevens | last post by:
Ok, the first thing i found out is that i should be using 'Public' instead of 'Global", so i did that. Here is what i tried to do (yes i have read as much as i could before i posted...... i...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.