How to initialize static class member

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

  1. subramanian

    subramanian Guest

    I am a beginner in C++.

    Suppose I want to build a class: I have given below the starting code:

    class Date {
    int day, month, year;

    static Date default_date;

    };

    Someone, kindly, completely tell me how to initialize(ie the definition
    of) the static member default_date in the above class and the
    constructors needed for this. Also how should I access the members of
    default_date ie the syntax to access it. I need the full
    implementation. I am asking this is for learning purpose.
     
    subramanian, Dec 31, 2006
    #1
    1. Advertising

  2. subramanian

    Salt_Peter Guest

    subramanian wrote:
    > I am a beginner in C++.
    >
    > Suppose I want to build a class: I have given below the starting code:
    >
    > class Date {
    > int day, month, year;
    >
    > static Date default_date;
    >
    > };
    >
    > Someone, kindly, completely tell me how to initialize(ie the definition
    > of) the static member default_date in the above class and the
    > constructors needed for this. Also how should I access the members of
    > default_date ie the syntax to access it. I need the full
    > implementation. I am asking this is for learning purpose.


    #include <iostream>
    #include <ostream>

    class Date {
    int day, month, year;
    static Date default_date;
    public:
    Date() : day(default_date.day),
    month(default_date.month),
    year(default_date.year) { }
    explicit Date(int d, int m, int y)
    : day(d), month(m), year(y) { }
    };

    Date Date::default_date(1,1,2000); // static member

    int main()
    {
    Date date;
    Date today(31,12,2006);
    }

    ___
    But then wouldn't be simpler do the above in the default ctor directly
    instead of using a static member?

    class Date {
    int day, month, year;
    public:
    Date() : day(1),
    month(1),
    year(2000) { }
    explicit Date(int d, int m, int y)
    : day(d), month(m), year(y) { }
    };
     
    Salt_Peter, Dec 31, 2006
    #2
    1. Advertising

  3. subramanian

    Rolf Magnus Guest

    Salt_Peter wrote:

    > explicit Date(int d, int m, int y)
    > : day(d), month(m), year(y) { }


    No point in making that constructor explicit. It isn't a conversion
    constructor anyway.
     
    Rolf Magnus, Dec 31, 2006
    #3
  4. subramanian

    subramanian Guest

    > But then wouldn't be simpler do the above in the default ctor directly
    > instead of using a static member?
    >
    > class Date {
    > int day, month, year;
    > public:
    > Date() : day(1),
    > month(1),
    > year(2000) { }
    > explicit Date(int d, int m, int y)
    > : day(d), month(m), year(y) { }
    >


    Thanks for the explanation. I am not able to understand this question
    and the above code ? Also, the static member is missing. Can you kindly
    explain ?
     
    subramanian, Dec 31, 2006
    #4
  5. subramanian

    Daniel T. Guest

    In article <>,
    "subramanian" <> wrote:

    > I am a beginner in C++.
    >
    > Suppose I want to build a class: I have given below the starting code:
    >
    > class Date {
    > int day, month, year;
    >
    > static Date default_date;
    >
    > };
    >
    > Someone, kindly, completely tell me how to initialize(ie the definition
    > of) the static member default_date in the above class and the
    > constructors needed for this.


    Date Date::default_date;

    > Also how should I access the members of
    > default_date ie the syntax to access it. I need the full
    > implementation. I am asking this is for learning purpose.


    void printDate( const Date& date )
    {
    cout << date.month << '/' << date.day << '/' << date.year;
    }

    int main() {

    // assignment
    Date::default_date.day = 31;
    Date::default_date.month = 12;
    Date::default_date.year = 2006;

    // reading
    int d = Date::default_date.day;
    int m = Date::default_date.month;
    int y = Date::default_date.year;

    // pass to function
    printDate( Date::default_date );
    }
     
    Daniel T., Dec 31, 2006
    #5
  6. subramanian

    Ron Natalie Guest

    subramanian wrote:
    >> But then wouldn't be simpler do the above in the default ctor directly
    >> instead of using a static member?
    >>
    >> class Date {
    >> int day, month, year;
    >> public:
    >> Date() : day(1),
    >> month(1),
    >> year(2000) { }
    >> explicit Date(int d, int m, int y)
    >> : day(d), month(m), year(y) { }
    >>

    >
    > Thanks for the explanation. I am not able to understand this question
    > and the above code ? Also, the static member is missing. Can you kindly
    > explain ?
    >


    By the way, explicit is only useful on a converting (that is, one
    that can be called with only one argument) constructor.
     
    Ron Natalie, Dec 31, 2006
    #6
  7. subramanian

    subramanian Guest

    Hello Daniel . T.

    You have mentioned

    Date Date::default_date;

    Is the above statement the definition of the memeber : static Date
    default_date.
    (I read somewhere each static member should be defined.)

    Here is the default constructor called ? But we have not provided it.
    So will the compiler supply it ?

    Thanks
     
    subramanian, Jan 1, 2007
    #7
  8. subramanian

    Daniel T. Guest

    "subramanian" <> wrote:

    > Hello Daniel . T.
    >
    > You have mentioned
    >
    > Date Date::default_date;
    >
    > Is the above statement the definition of the memeber : static Date
    > default_date.


    Yes.

    > Here is the default constructor called ?


    Yes.

    > But we have not provided it. So will the compiler supply it ?


    Yes, but the compiler supplied default constructor will do nothing. It
    will not even initialize the values to 0.
     
    Daniel T., Jan 1, 2007
    #8
  9. subramanian

    John Femiani Guest

    Guys,

    There are a couple ways to initialize static members that I know of:
    1) declare the static variable in the class, and initilize it exactly
    once inside of a .cpp (or cxx or whatever) file. You generally can't do
    that in a header because it might get included by more than one cpp
    file and the linker won't know what to do.
    2) if it is a const integer, you can sometimes do it inline with the
    class (depends on the compiler I think)
    3) if it is an constant ordinal type, you can use an enumeration.
    4) you can wrap it inside of a function;

    static Data& date_thing() {static int value = initial_value; return
    value;}


    John
     
    John Femiani, Jan 2, 2007
    #9
  10. subramanian

    Salt_Peter Guest

    John Femiani wrote:
    > Guys,
    >
    > There are a couple ways to initialize static members that I know of:
    > 1) declare the static variable in the class, and initilize it exactly
    > once inside of a .cpp (or cxx or whatever) file. You generally can't do
    > that in a header because it might get included by more than one cpp
    > file and the linker won't know what to do.
    > 2) if it is a const integer, you can sometimes do it inline with the
    > class (depends on the compiler I think)
    > 3) if it is an constant ordinal type, you can use an enumeration.
    > 4) you can wrap it inside of a function;
    >
    > static Data& date_thing() {static int value = initial_value; return
    > value;}
    >
    >
    > John


    The point here is that a constructor must still initialize the members
    of the static variable.
    Hence, the need for a default ctor, in which case having a static
    default_date member is a moot point. Simply initialize the members in a
    default ctor.
     
    Salt_Peter, Jan 3, 2007
    #10
    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. Siemel Naran
    Replies:
    4
    Views:
    818
    Micah Cowan
    Jan 12, 2005
  2. Jim Langston
    Replies:
    5
    Views:
    381
    Jim Langston
    Sep 23, 2005
  3. Replies:
    3
    Views:
    864
    Peter_Julian
    Oct 10, 2005
  4. Nan Li
    Replies:
    7
    Views:
    560
    Nan Li
    Nov 9, 2005
  5. dolphin
    Replies:
    3
    Views:
    1,366
    Pete Becker
    Dec 5, 2007
Loading...

Share This Page