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

Global variables...

Hello,

Say I have a class with a member...

char mId;

Whenever an object is created, I want to assign an incrementing
character to this member, for instance the first would be A, then B, C,
D, E etc....

Unless I create a singleton class as a provider for this, the only way
I can think of tracking the last ID would be to use a global variable.
Is there a way to do this without either of the above?

Background: We are in the middle of renovating the entire structure of
our software. A customer needs the current build for other fixes
however at the moment I can not access the methods of a particular
class. A global would give me a temporary workaround to access the
member directly... This is probably not enough info to help out but...
(The actualy member is not a char, but for the purpose of this
question it will suffice.

Dec 22 '05 #1
8 1784
On 22 Dec 2005 13:10:46 -0800 in comp.lang.c++, "Morpheus"
<ad***********@gmail.com> wrote,
Whenever an object is created, I want to assign an incrementing
character to this member, for instance the first would be A, then B, C,
D, E etc....

Unless I create a singleton class as a provider for this, the only way
I can think of tracking the last ID would be to use a global variable.


I would guess that the last ID would ordinarily be a static member
variable.

Dec 22 '05 #2
David Harmon wrote:
On 22 Dec 2005 13:10:46 -0800 in comp.lang.c++, "Morpheus"
<ad***********@gmail.com> wrote,
Whenever an object is created, I want to assign an incrementing
character to this member, for instance the first would be A, then B, C,
D, E etc....

Unless I create a singleton class as a provider for this, the only way
I can think of tracking the last ID would be to use a global variable.


I would guess that the last ID would ordinarily be a static member
variable.


Right. Something like:

class A
{
public:
A() : mID( sID++ ) {}
private:
char mID;
static char sID;
};

char A::sID=0;

Cheers! --M

Dec 22 '05 #3
Morpheus wrote:
Say I have a class with a member...

char mId;

Whenever an object is created, I want to assign an incrementing
character to this member, for instance the first would be A, then B, C,
D, E etc....

Unless I create a singleton class as a provider for this, the only way
I can think of tracking the last ID would be to use a global variable.
Is there a way to do this without either of the above?
A static data member of a class is essentially a hidden global variable.
However, given that it has advantages over a global variable, I'd go that
route, probably.
[.."explanation" of "background" removed -- made no sense anyway..]


V
Dec 22 '05 #4
Morpheus wrote:
Hello,

Say I have a class with a member...

char mId;

Whenever an object is created, I want to assign an incrementing
character to this member, for instance the first would be A, then B, C,
D, E etc....

Unless I create a singleton class as a provider for this, the only way
I can think of tracking the last ID would be to use a global variable.
Is there a way to do this without either of the above?

Background: We are in the middle of renovating the entire structure of
our software. A customer needs the current build for other fixes
however at the moment I can not access the methods of a particular
class. A global would give me a temporary workaround to access the
member directly... This is probably not enough info to help out but...
(The actualy member is not a char, but for the purpose of this
question it will suffice.


The member type is more important then you may think.
If the type is NOT a POD type, and you have other global comlex types
trying to access them, you're going to run into problems in that the ID
types may or may not be initialized when called before main() starts.
That's because the C++ standard does not garantee the order of
construction for complex objects in multiple translation units.
Global POD types are garanteed to be initialized before global non-POD
types.
So if your ID type is an int type, and you have a global foo type that
tries to access the ID, then you should be safe. But if your ID is a
static std::string member, and you have a global foo type that tries to
access the ID (before main() starts), then you're going to run into
problems, because you have no garantee that the ID variable has been
initialized.

You also have a similar related problem when your application tries to
exit. You could have a static member variable (static std::string ID)
who's destructor has been called, and then have a global type try to
access it after it's been destroyed.

One easy safe workaround solution is to use a function that stores the
complex object as a local static variable.

std::string& GetID()
{
static std::string *strID = new std::string();
return *strID;
}

IAW C++ standard this would garantee that the object would be created
when fetching it from multiple translation (modules) units (via global
object).

You can also use a method MS used with it's CString class for default
emtpy CString's, but I'm not sure how portable that method would be.

Dec 22 '05 #5
On Thu, 22 Dec 2005 13:28:49 -0800, mlimber wrote:
David Harmon wrote:
On 22 Dec 2005 13:10:46 -0800 in comp.lang.c++, "Morpheus"
<ad***********@gmail.com> wrote,
>Whenever an object is created, I want to assign an incrementing
>character to this member, for instance the first would be A, then B, C,
>D, E etc....
>
>Unless I create a singleton class as a provider for this, the only way
>I can think of tracking the last ID would be to use a global variable.


I would guess that the last ID would ordinarily be a static member
variable.


Right. Something like:

class A
{
public:
A() : mID( sID++ ) {}
private:
char mID;
static char sID;
};

char A::sID=0;


That works. Here's another similar way:

class A
{
public:
A() : mID( GetNextID() ) {}
private:

static char GetNextID()
{
static char sID = 0;
return SID++;
}

char mID;
};

- Jay
Dec 22 '05 #6
Jay Nabonne wrote:
[..]
static char GetNextID()
{
static char sID = 0;
return SID++;
return sID++;

}
[..]

Dec 23 '05 #7
Good... thanks for the help. I was originally thinking of a static
member but thought that setting it in one object would affect the
other... using a static variable within a method seems to do the trick,
at least as a workaround and does not "seem" to break any rules.
[.."explanation" of "background" removed -- made no sense anyway..]


Hence my disclaimer "This is probably not enough info to help out
but... " :)

Dec 23 '05 #8
On Fri, 23 Dec 2005 08:15:00 -0800, Morpheus wrote:
Good... thanks for the help. I was originally thinking of a static
member but thought that setting it in one object would affect the
other... using a static variable within a method seems to do the trick,
at least as a workaround and does not "seem" to break any rules.
[.."explanation" of "background" removed -- made no sense anyway..]


Hence my disclaimer "This is probably not enough info to help out
but... " :)


The *behavior* should be equivalent (class static vs. local static vs
global). It's just a question of where the persistent variable is (i.e.
it's a design question). The whole point of the persistent variable is
that it *does* affect other objects (every object of that class created,
in fact).

- Jay

Dec 23 '05 #9

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...
4
by: Andrew V. Romero | last post by:
I have been working on a function which makes it easier for me to pull variables from the URL. So far I have: <script language="JavaScript"> var variablesInUrl; var vArray = new Array(); ...
12
by: David WOO | last post by:
Hi, I am a newbie on C++, I need to define some global variables which should be accessible to most classes. In the mean time, I don't won't the global variables be modified freely at most of...
2
by: Bryan Parkoff | last post by:
….I would like to know which is the best optimization to use global variable or global struct. I always tell C/C++ Compiler to turn on optimization. ….I use underscore between first name and...
17
by: MLH | last post by:
A97 Topic: If there is a way to preserve the values assigned to global variables when an untrapped runtime error occurs? I don't think there is, but I thought I'd ask. During development, I'm...
33
by: MLH | last post by:
I've read some posts indicating that having tons of GV's in an Access app is a bad idea. Personally, I love GVs and I use them (possibly abuse them) all the time for everything imaginable - have...
9
by: CDMAPoster | last post by:
About a year ago there was a thread about the use of global variables in A97: http://groups.google.com/group/comp.databases.ms-access/browse_frm/thread/fedc837a5aeb6157 Best Practices by Kang...
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...
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...
112
by: istillshine | last post by:
When I control if I print messages, I usually use a global variable "int silent". When I set "-silent" flag in my command line parameters, I set silent = 1 in my main.c. I have many functions...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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...
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...

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.