 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
subramanian Guest
|
Posted: Sat Dec 30, 2006 10:10 am Post subject: how to initialize static member |
|
|
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 ? |
|
| Back to top |
|
 |
Salt_Peter Guest
|
Posted: Sat Dec 30, 2006 10:10 am Post subject: Re: how to initialize static member |
|
|
subramanian wrote:
| Quote: | 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. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|