473,237 Members | 1,235 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,237 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 27354

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:
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.