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

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 27364

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_date.day),
month(default_date.month),
year(default_date.year) { }
explicit Date(int d, int m, int y)
: day(d), month(m), year(y) { }
};

Date Date::default_date(1,1,2000); // static member

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

___
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*********************@s34g2000cwa.googlegroups. com>,
"subramanian" <su**************@yahoo.comwrote:
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_date;
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_date.day = 31;
Date::default_date.month = 12;
Date::default_date.year = 2006;

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

// pass to function
printDate( Date::default_date );
}
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_date;

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
"subramanian" <su**************@yahoo.comwrote:
Hello Daniel . T.

You have mentioned

Date Date::default_date;

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
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...
3
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
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...
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...
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...
5
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.