how to initialize static member

Discussion in 'C++' started by subramanian, Dec 30, 2006.

  1. subramanian

    subramanian Guest

    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 ?
     
    subramanian, Dec 30, 2006
    #1
    1. Advertising

  2. subramanian

    Salt_Peter Guest

    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.
     
    Salt_Peter, Dec 30, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. cppaddict
    Replies:
    16
    Views:
    19,839
    m_pahlevanzadeh
    Jul 11, 2012
  2. Jim Langston
    Replies:
    5
    Views:
    379
    Jim Langston
    Sep 23, 2005
  3. Replies:
    3
    Views:
    862
    Peter_Julian
    Oct 10, 2005
  4. Nan Li
    Replies:
    7
    Views:
    559
    Nan Li
    Nov 9, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,362
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page