initialize static member variable

M

markww

Hi,

I put a static member variable in my class.

class CMine {
static int m_nCount;
};

How do I initialize it to zero? I can't do that in the constructor of
the class can I? Won't that keep setting it to zero everytime a new
instance is created?

Thanks
 
T

Thomas Tutone

markww said:
Hi,

I put a static member variable in my class.

class CMine {
static int m_nCount;

change the above line to:

static int m_nCount = 0;

Also, in a .cpp file somewhere, add the following line:

int CMine::m_nCount;
How do I initialize it to zero?

Do so in the class definition as shown above.
I can't do that in the constructor of
the class can I?
No.

Won't that keep setting it to zero everytime a new
instance is created?

Yep.

Best regards,

Tom
 
A

Alf P. Steinbach

* markww:
Hi,

I put a static member variable in my class.

class CMine {
static int m_nCount;
};

How do I initialize it to zero?

Like

int CMine::m_nCount = 0;

Or you could just declare it with no explicit initialization since
static variables are zero-initialized before any other initialization
(and there is no other initialization for an 'int' with no explicit
initialization).

By the way, I'd remove the Microsoft Hungarian notation prefixes. For
the class the prefix adds more to write and reduces readability. For
the static variable the prefix is directly misleading.

I can't do that in the constructor of the class can I?

Technically you can, but that would in practice constrain the usage of
the class, and it would be unexpected.

Won't that keep setting it to zero everytime a new instance is created?

Yes.
 
A

Alf P. Steinbach

* Thomas Tutone:
change the above line to:

static int m_nCount = 0;


Also, in a .cpp file somewhere, add the following line:

int CMine::m_nCount;


Do so in the class definition as shown above.

Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:48) for ONLINE_EVALUATION_Alpha1
Copyright 1988-2006 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 2: error: data member initializer is not allowed
Wild guess: Did you want static const and not just static?
Wild guess: Your initializer is for a floating type (which is
not allowed)
static int m_nCount = 0;
^

1 error detected in the compilation of "ComeauTest.c".
 
T

Thomas Tutone

Alf said:
* Thomas Tutone:

Comeau C/C++ 4.3.8 (Aug 19 2006 13:36:48) for ONLINE_EVALUATION_Alpha1
Copyright 1988-2006 Comeau Computing. All rights reserved.
MODE:strict errors C++

"ComeauTest.c", line 2: error: data member initializer is not allowed
Wild guess: Did you want static const and not just static?
Wild guess: Your initializer is for a floating type (which is
not allowed)
static int m_nCount = 0;
^

1 error detected in the compilation of "ComeauTest.c".

Ugh. Absolutely right - I was thinking of a static const int member.
To the OP - follow Alf Steinbach's advice, and include the following
line in a .cpp file:

int CMine::m_nCount = 0;

Don't make the change I indicated to the class definition.

Sorry for the confusion.

Best regards,

Tom
 
M

markww

Alf said:
* markww:

Like

int CMine::m_nCount = 0;

Or you could just declare it with no explicit initialization since
static variables are zero-initialized before any other initialization
(and there is no other initialization for an 'int' with no explicit
initialization).

By the way, I'd remove the Microsoft Hungarian notation prefixes. For
the class the prefix adds more to write and reduces readability. For
the static variable the prefix is directly misleading.



Technically you can, but that would in practice constrain the usage of
the class, and it would be unexpected.



Yes.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

Ok looks good, its working as expected now, thanks guys,

Mark
 
S

Salt_Peter

markww said:
Hi,

I put a static member variable in my class.

class CMine {
static int m_nCount;
};

How do I initialize it to zero? I can't do that in the constructor of
the class can I? Won't that keep setting it to zero everytime a new
instance is created?

Thanks

The member variable is static, so regardless of how many instances of
CMine you have, doesn't it make sense that it needs to be initialized
once outside of the class?

#include <iostream>
#include <ostream>

class CMine
{
static int m_nCount;
public:
static int count() { return m_nCount; }
};

int CMine::m_nCount = 0; // or whatever value

int main()
{
CMine cmine;
std::cout << "count = " << cmine.count();
std::cout << std::endl;

return 0;
}

/*
count = 0
*/
 

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,995
Messages
2,570,236
Members
46,824
Latest member
Nater888

Latest Threads

Top