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

how to initialize static member

P: n/a
I saw the following code in Bjarne Stroustrup's 3rd edition of the book
The C++ Programming Language(NOT the special 3rd edition) - Pages228
and 229, related to static members.

I am giving the code only partly:

class Date {

int d, m, y;
static Date default_date;

public:
Date(int dd=0, int mm=0, int yy=0);
....
};

Date::Date(int dd, int mm, int yy)
{
....
}

Date Date::default_date(16, 12, 1770);
--------------------------------------------------------------

MY DOUBTS:
Here, does default_date(16, 12, 1770) call the constructor
Date::Date(int dd, int mm, int yy) ?

Is contructor called for static memebrs ?

Dec 30 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

subramanian wrote:
I saw the following code in Bjarne Stroustrup's 3rd edition of the book
The C++ Programming Language(NOT the special 3rd edition) - Pages228
and 229, related to static members.

I am giving the code only partly:

class Date {

int d, m, y;
static Date default_date;

public:
Date(int dd=0, int mm=0, int yy=0);
...
};

Date::Date(int dd, int mm, int yy)
: d(dd), m(mm), y(yy) // missing init list
{
...
}

Date Date::default_date(16, 12, 1770);
--------------------------------------------------------------

MY DOUBTS:
Here, does default_date(16, 12, 1770) call the constructor
Date::Date(int dd, int mm, int yy) ?

Is contructor called for static memebrs ?
Yes, it invokes the ctor.
The static member's ctor can only be invoked that way.
The init list is crucial.
replace with the following and observe...
#include <iostream>
....
Date::Date(int dd, int mm, int yy)
: d(dd), m(mm), y(yy) // missing init list
{
std::cout << "Date(int, int, int)\n";
}
You should declare a default date in main to see how that static member
is used to default initialize an instance of the class - however, you
did not show that part of the code.

Dec 30 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.