C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

how to initialize static member

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
subramanian
Guest





PostPosted: Sat Dec 30, 2006 10:10 am    Post subject: how to initialize static member Reply with 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)
{
....
}

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





PostPosted: Sat Dec 30, 2006 10:10 am    Post subject: Re: how to initialize static member Reply with quote



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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.