Connecting Tech Pros Worldwide Forums | Help | Site Map

How to initialize static class member

subramanian
Guest
 
Posts: n/a
#1: Dec 31 '06
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.


Salt_Peter
Guest
 
Posts: n/a
#2: Dec 31 '06

re: How to initialize static class member



subramanian wrote:
Quote:
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) { }
};

Rolf Magnus
Guest
 
Posts: n/a
#3: Dec 31 '06

re: How to initialize static class member


Salt_Peter wrote:
Quote:
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.

subramanian
Guest
 
Posts: n/a
#4: Dec 31 '06

re: How to initialize static class member


But then wouldn't be simpler do the above in the default ctor directly
Quote:
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 ?

Daniel T.
Guest
 
Posts: n/a
#5: Dec 31 '06

re: How to initialize static class member


In article <1167544548.293950.72530@s34g2000cwa.googlegroups. com>,
"subramanian" <subramanian100in@yahoo.comwrote:
Quote:
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;
Quote:
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 );
}
Ron Natalie
Guest
 
Posts: n/a
#6: Dec 31 '06

re: How to initialize static class member


subramanian wrote:
Quote:
Quote:
>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.
subramanian
Guest
 
Posts: n/a
#7: Jan 1 '07

re: How to initialize static class member


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

Daniel T.
Guest
 
Posts: n/a
#8: Jan 1 '07

re: How to initialize static class member


"subramanian" <subramanian100in@yahoo.comwrote:
Quote:
Hello Daniel . T.
>
You have mentioned
>
Date Date::default_date;
>
Is the above statement the definition of the memeber : static Date
default_date.
Yes.
Quote:
Here is the default constructor called ?
Yes.
Quote:
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.
John Femiani
Guest
 
Posts: n/a
#9: Jan 2 '07

re: How to initialize static class member



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

Salt_Peter
Guest
 
Posts: n/a
#10: Jan 3 '07

re: How to initialize static class member



John Femiani wrote:
Quote:
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.

Closed Thread