Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to initialize static member

Reply
Thread Tools

how to initialize static member

 
 
subramanian
Guest
Posts: n/a
 
      12-30-2006
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:ate(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:ate(int dd, int mm, int yy) ?

Is contructor called for static memebrs ?

 
Reply With Quote
 
 
 
 
Salt_Peter
Guest
Posts: n/a
 
      12-30-2006

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:ate(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:ate(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:ate(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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
best way to initialize static member objects cppaddict C++ 16 07-11-2012 11:28 AM
Can a static member function access non-static member? dolphin C++ 3 12-05-2007 12:39 PM
if instance variable get initialize after assigning some values or after constructor then when does static variable get initialize Tony Morris Java 3 02-04-2006 08:39 AM
How to initialize an array member in the member initialization list? jut_bit_zx@eyou.com C++ 3 10-10-2005 12:10 AM



Advertisments