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 6 17446
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
* 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?
* 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_EVALUATI ON_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?
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_EVALUATI ON_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
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
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
*/ This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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 int member:
MyClass::myStaticMemberInt = 99;
|
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 { lengthSV=16 }; // Length of SomeVector
vector<double> SomeVector;
|
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 know you wouldn't use 'C'
{
private:
static std::vector<int> PSArray;
public:
|
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
private string myArray;
|
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;
};
| |
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 should initialize them in advance. However, the following
code, in which I first produce two classses and then try to assign a
reference of the first class to a static data member of the second class
doesn't work. It gives the following compiler error:
|
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 an instance of the class bla. According to a previous posting of
Zeppe on 4/27...
Doing that, this is my code:
class bla
|
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 C++ apparently doesn't allow a static
constructor routine, how do I initialize my static member variable prior to
allowing any instances to be constructed?
TIA - Bob
|
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 to put them in
main(), it's so far away.
Thanks.
-
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |