By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,405 Members | 1,059 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,405 IT Pros & Developers. It's quick & easy.

How to initialize static class member

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a

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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
"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

P: n/a

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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.