473,385 Members | 2,029 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,385 software developers and data experts.

initialize static member variable

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

Aug 26 '06 #1
6 17417
markww wrote:
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

Aug 26 '06 #2
* 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: 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?
Aug 26 '06 #3
* Thomas Tutone:
markww wrote:
>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.
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".

--
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?
Aug 26 '06 #4
Alf P. Steinbach wrote:
* Thomas Tutone:
markww wrote:
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.

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

Aug 26 '06 #5

Alf P. Steinbach wrote:
* 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: 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

Aug 26 '06 #6

markww wrote:
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
*/

Aug 26 '06 #7

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

Similar topics

15
by: cppaddict | last post by:
I have class with two static member objects, one of type int and one of type vector<int>. static int myStaticMemberInt static vector<int> myStaticMemberVector; I know how to initialize the...
10
by: Fred Ma | last post by:
Are there any reasons that would make it bad for C++ to allow simultaneous declaration and initilization of member data? Current way: ------------ class DerivedClass : BaseClass { { enum {...
5
by: Jim Langston | last post by:
What I want to do: have a vector of ints in my class initialized with 0 to 499 which will later be pushed/popped out of the vector by instances. What I have: class CParticleStream // Yes, I...
15
by: Geoff Cox | last post by:
Hello, Can I separately declare and initialize a string array? How and where would I do it in the code below? It was created using Visual C++ 2005 Express Beta 2 ... In C# I would have ...
9
by: subramanian | last post by:
I am a beginner in C++. Suppose I want to build a class: I have given below the starting code: class Date { int day, month, year; static Date default_date; };
4
by: Bram Kuijper | last post by:
Hi all, as a C++ newbie, I got some question on the initialization of static reference data members. Since it isn't possible to initialize static members of a class in the constructor, I...
4
by: Bram Kuijper | last post by:
Okay, second try (since my posting on 4/27), to find a proper way to initialize a static reference member to an object. I try to initialize a static reference inside the class ga, referencing to...
5
by: Bob Altman | last post by:
Hi all, I have a private static structure in a C++ class (it's a CRITICAL_SECTION structure) that needs to be initialized by passing its address to a routine (InitializeCriticalSection). Since...
9
by: Steven Woody | last post by:
Hi, Supposing a class get a complicated static member foo, and it need to be initialized before any method of the class can be called, where should I put these initialization code? I don't want...
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: 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: 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
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
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...

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.