Inconsistent Global Variables

M

mongoose7

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();
};
 
J

Jonathan Mcdougall

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
 
R

Rade

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
 
R

Rade

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,017
Latest member
GreenAcreCBDGummiesReview

Latest Threads

Top