473,394 Members | 1,715 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.

Inconsistent Global Variables

Hello everyone.

I wrote a small class to keep some global variables updated. I use a
static method to get the single instance of this class in 2 places in
the code. In one place the global variable is being incremented in the
other it stays at zero...

I dont understand whats going wrong sice the same instance is being
called through the getInstance method... (i think ;) ). Its as if a new
object is being created every time in the second place where im calling
the methods. Any ideas?

Code below.

Thanks!!!

Mongoose7
class CDynamicGlobal
{
public:
// The CDynamicGlobal is treated as a single object using the
// singleton design pattern

static CDynamicGlobal& getInstance()
{
static CDynamicGlobal pInstance;
return pInstance;
}
// Increment the Frame counter
void incFrameNumber();

// Get the current frame number
long getFrameNumber();

// Initialize all the counters
void init();
private:
long __frameNumber;

CDynamicGlobal();
};

Jul 23 '05 #1
3 1548
> I wrote a small class to keep some global variables updated. I use a
static method to get the single instance of this class in 2 places in
the code. In one place the global variable is being incremented in the other it stays at zero... I dont understand whats going wrong sice the same instance is being
called through the getInstance method... (i think ;) ). Its as if a new object is being created every time in the second place where im calling the methods. Any ideas?


You did not disable the copy constructor nor the assignement operator.
It may be that something like

CDynamicGlobal g = CDynamicGlobal::getInstance();

appears in your code, which makes a copy of the "single" instance.

If that does not solve the problem, post the class' member functions'
implementations along with the two places where you it them.
Jonathan

Jul 23 '05 #2
> static CDynamicGlobal& getInstance()
{
static CDynamicGlobal pInstance;
return pInstance;
}


This question has inspired me to ask the community (i.e. somebody who knows
the nuts and bolts of the C++ Standard) the following question:

In the example provided, the method getInstance() is defined inside the
class. Does it mean that it is implicitly inline, and further, that the
compiler is then free to create a separate copy of the function (instead of
actual inlining) in each translation unit where the header is included? If
this is the case, you may really have a business with multiple singletons -
one in each translation unit.

Or, maybe, I am wrong and the compiler is obliged to create a single copy of
the function (or multiple copies which are eliminated by the linker or by
some other mechanism)?

If I am right, the cure might be simply to create a separate translation
unit and define getInstance() in it instead of defining inside the class.

If I am wrong, why am I (i.e. which paragraph of the Standard do I have to
read)?

Rade
Jul 23 '05 #3
> This question has inspired me to ask the community (i.e. somebody who
knows the nuts and bolts of the C++ Standard) the following question:

In the example provided, the method getInstance() is defined inside the
class. Does it mean that it is implicitly inline, and further, that the
compiler is then free to create a separate copy of the function (instead
of actual inlining) in each translation unit where the header is included?
If this is the case, you may really have a business with multiple
singletons - one in each translation unit.

Or, maybe, I am wrong and the compiler is obliged to create a single copy
of the function (or multiple copies which are eliminated by the linker or
by some other mechanism)?

If I am right, the cure might be simply to create a separate translation
unit and define getInstance() in it instead of defining inside the class.

If I am wrong, why am I (i.e. which paragraph of the Standard do I have to
read)?


I am sorry for wasting everybody's time. I am wrong and the paragraph
explaining it is 9.3 [6].

Rade
Jul 23 '05 #4

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(); ...
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...
4
by: carl.dhalluin | last post by:
Hello I am completely puzzled why the following exec code does not work: mycode = "import math\ndef f(y):\n print math.floor(y)\nf(3.14)" def execute(): exec mycode execute()
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.