473,394 Members | 1,935 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,394 software developers and data experts.

Variables in global data space

I have a string variable g_appname which should be global accessable by
any class who whishes. I have defined it as

wxString *g_appname = NULL;

and fills it later on. Now a friend asked why I didn't write

wxString g_appname = "Name";

I had to answer "I don't know", I just had the impression in global it
should be a reference. Again when he asked why I didn't use a static
variable in a class, I didn't know an answer beside not knowing which
class it belongs to. So what's correct in which case?

O. Wyss

--
See "http://wxguide.sourceforge.net/" for ideas how to design your app.
Jul 19 '05 #1
2 3051

"Otto Wyss" <wy*@users.sourceforge.net> wrote in message
news:1g0q781.m4i6fn7vuqvqN%wy*@users.sourceforge.n et...
I have a string variable g_appname which should be global accessable by
any class who whishes. I have defined it as

wxString *g_appname = NULL;

and fills it later on. Now a friend asked why I didn't write

wxString g_appname = "Name";

I had to answer "I don't know", I just had the impression in global it
should be a reference. Again when he asked why I didn't use a static
variable in a class, I didn't know an answer beside not knowing which
class it belongs to. So what's correct in which case?

O. Wyss


There are no restrictions on what types a global variable can have.

wxString g_appname = "Name";

is fine. However there is the issue of which order global variables are
constructed in. Its perfectly possible to write code where the construction
of one global variable assumes that another global variable has been
constructed first. But except for the case where the two global variables
are in the same file it is not possible to say in which order two global
variables will be constructed.

Your code that uses a pointer does not suffer from this problem. It is
guaranteed to be NULL at the moment the program starts. The constructors of
other global variables can safely assume this to be true.

As for the static variable in a class argument. Its not possible to know
whether that a good idea without seeing the design of your code.

john
Jul 19 '05 #2
John Harrison <jo*************@hotmail.com> wrote:
wxString *g_appname = NULL;

and fills it later on. Now a friend asked why I didn't write

wxString g_appname = "Name";


There are no restrictions on what types a global variable can have.

wxString g_appname = "Name";

is fine. However there is the issue of which order global variables are
constructed in. Its perfectly possible to write code where the construction
of one global variable assumes that another global variable has been
constructed first. But except for the case where the two global variables
are in the same file it is not possible to say in which order two global
variables will be constructed.

Your code that uses a pointer does not suffer from this problem. It is
guaranteed to be NULL at the moment the program starts. The constructors of
other global variables can safely assume this to be true.

As for the static variable in a class argument. Its not possible to know
whether that a good idea without seeing the design of your code.

In other words, it doesn't matter much unless there are dependences
between global variables.

You may look at the full source here:
"http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/wxguide/wxGuide/editor/s
rc/app.cpp?rev=1.104&content-type=text/vnd.viewcvs-markup".

Thanks, O. Wyss

--
See "http://wxguide.sourceforge.net/" for ideas how to design your app.
Jul 19 '05 #3

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

Similar topics

10
by: Matt | last post by:
Greetings, What are people's thoughts on global variables in C++? Why are we taught not to use them in programming? Is it true that if you are running two copies of the C program one copy can...
2
by: Anand Subramanian | last post by:
Hi, Can someone explain the differences(setup, pre-main() setup/initialization) between global variables in a C++ and a C program? The global variables I used are uninitialized. I have a test.o...
115
by: Mark Shelor | last post by:
I've encountered a troublesome inconsistency in the C-language Perl extension I've written for CPAN (Digest::SHA). The problem involves the use of a static array within a performance-critical...
27
by: Madhav | last post by:
Hi all, I did not understand why do the global vars are initialized to NULL where as the block level variables have random values? I know that the C standard requires this as was mentioned in a...
5
by: Sandman | last post by:
I dont think I understand them. I've read the section on scope in the manual inside out. I'm running PHP 5.2.0 Here is the code I'm working on: //include_me.php <?php $MYVAR = array(); global...
55
by: Zytan | last post by:
I see that static is more restricted in C# than in C++. It appears usable only on classes and methods, and data members, but cannot be created within a method itself. Surely this is possible in...
1
weaknessforcats
by: weaknessforcats | last post by:
C++: The Case Against Global Variables Summary This article explores the negative ramifications of using global variables. The use of global variables is such a problem that C++ architects have...
1
by: Rahul | last post by:
Hi Everyone, It is known that static member variables can be accessed without creating an instance of the class and it serves as a global variable without polluting the global name space. So...
5
by: vnpatriot7 | last post by:
Hi everybody, I have two questions: 1) About memory space in C++ 2) About global and static variable As what I read somewhere that after compilation process which translate C++ code into...
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.