473,657 Members | 2,380 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to initialize static class member

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;

};

Someone, kindly, completely tell me how to initialize(ie the definition
of) the static member default_date in the above class and the
constructors needed for this. Also how should I access the members of
default_date ie the syntax to access it. I need the full
implementation. I am asking this is for learning purpose.

Dec 31 '06 #1
9 27379

subramanian wrote:
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;

};

Someone, kindly, completely tell me how to initialize(ie the definition
of) the static member default_date in the above class and the
constructors needed for this. Also how should I access the members of
default_date ie the syntax to access it. I need the full
implementation. I am asking this is for learning purpose.
#include <iostream>
#include <ostream>

class Date {
int day, month, year;
static Date default_date;
public:
Date() : day(default_dat e.day),
month(default_d ate.month),
year(default_da te.year) { }
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }
};

Date Date::default_d ate(1,1,2000); // static member

int main()
{
Date date;
Date today(31,12,200 6);
}

___
But then wouldn't be simpler do the above in the default ctor directly
instead of using a static member?

class Date {
int day, month, year;
public:
Date() : day(1),
month(1),
year(2000) { }
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }
};

Dec 31 '06 #2
Salt_Peter wrote:
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }
No point in making that constructor explicit. It isn't a conversion
constructor anyway.

Dec 31 '06 #3
But then wouldn't be simpler do the above in the default ctor directly
instead of using a static member?

class Date {
int day, month, year;
public:
Date() : day(1),
month(1),
year(2000) { }
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }
Thanks for the explanation. I am not able to understand this question
and the above code ? Also, the static member is missing. Can you kindly
explain ?

Dec 31 '06 #4
In article <11************ *********@s34g2 000cwa.googlegr oups.com>,
"subramania n" <su************ **@yahoo.comwro te:
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;

};

Someone, kindly, completely tell me how to initialize(ie the definition
of) the static member default_date in the above class and the
constructors needed for this.
Date Date::default_d ate;
Also how should I access the members of
default_date ie the syntax to access it. I need the full
implementation. I am asking this is for learning purpose.
void printDate( const Date& date )
{
cout << date.month << '/' << date.day << '/' << date.year;
}

int main() {

// assignment
Date::default_d ate.day = 31;
Date::default_d ate.month = 12;
Date::default_d ate.year = 2006;

// reading
int d = Date::default_d ate.day;
int m = Date::default_d ate.month;
int y = Date::default_d ate.year;

// pass to function
printDate( Date::default_d ate );
}
Dec 31 '06 #5
subramanian wrote:
>But then wouldn't be simpler do the above in the default ctor directly
instead of using a static member?

class Date {
int day, month, year;
public:
Date() : day(1),
month(1),
year(2000) { }
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }

Thanks for the explanation. I am not able to understand this question
and the above code ? Also, the static member is missing. Can you kindly
explain ?
By the way, explicit is only useful on a converting (that is, one
that can be called with only one argument) constructor.
Dec 31 '06 #6
Hello Daniel . T.

You have mentioned

Date Date::default_d ate;

Is the above statement the definition of the memeber : static Date
default_date.
(I read somewhere each static member should be defined.)

Here is the default constructor called ? But we have not provided it.
So will the compiler supply it ?

Thanks

Jan 1 '07 #7
"subramania n" <su************ **@yahoo.comwro te:
Hello Daniel . T.

You have mentioned

Date Date::default_d ate;

Is the above statement the definition of the memeber : static Date
default_date.
Yes.
Here is the default constructor called ?
Yes.
But we have not provided it. So will the compiler supply it ?
Yes, but the compiler supplied default constructor will do nothing. It
will not even initialize the values to 0.
Jan 1 '07 #8

Guys,

There are a couple ways to initialize static members that I know of:
1) declare the static variable in the class, and initilize it exactly
once inside of a .cpp (or cxx or whatever) file. You generally can't do
that in a header because it might get included by more than one cpp
file and the linker won't know what to do.
2) if it is a const integer, you can sometimes do it inline with the
class (depends on the compiler I think)
3) if it is an constant ordinal type, you can use an enumeration.
4) you can wrap it inside of a function;

static Data& date_thing() {static int value = initial_value; return
value;}
John

Jan 2 '07 #9

John Femiani wrote:
Guys,

There are a couple ways to initialize static members that I know of:
1) declare the static variable in the class, and initilize it exactly
once inside of a .cpp (or cxx or whatever) file. You generally can't do
that in a header because it might get included by more than one cpp
file and the linker won't know what to do.
2) if it is a const integer, you can sometimes do it inline with the
class (depends on the compiler I think)
3) if it is an constant ordinal type, you can use an enumeration.
4) you can wrap it inside of a function;

static Data& date_thing() {static int value = initial_value; return
value;}
John
The point here is that a constructor must still initialize the members
of the static variable.
Hence, the need for a default ctor, in which case having a static
default_date member is a moot point. Simply initialize the members in a
default ctor.

Jan 3 '07 #10

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

Similar topics

15
2865
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;
10
3516
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;
5
4555
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:
3
1660
by: Bill Sun | last post by:
Hi, I have a question about to initialize a static map member like this: In the mapclass.h; class mapclass { private: static map<string, int> s_mapArray; }
6
17435
by: markww | last post by:
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
4
8356
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:
4
1974
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
9
3405
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. -
5
7225
by: Timothy Madden | last post by:
Hy static members of non-integral type need to be declared in the class, but defined (and constructed or initialized) outside the class. Like this class SystemName { public:
0
8394
marktang
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...
0
8306
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,...
0
8825
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, 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...
1
8503
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,...
0
7327
agi2029
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...
0
5632
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
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...

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.