How are static objects, that are members of a class and that are of the same type as the class they are in, initialized?

StackOverflow https://stackoverflow.com/questions/21193724

  •  29-09-2022
  •  | 
  •  

سؤال

Bjarne Strousrup in The C++ Programming Language illustrates the use of static members of a class with code similar to this:

class Date{
  int d, m, y;
  static Date default_date;
  public:
      Date(int dd=0, int mm=0, int yy=0);
      static void set_default(int, int, int);
      int year()const{return y;}
      int month() const{return m;}
      int day() const {return d;}

};
Date::Date(int dd, int mm, int yy){
d=dd ? dd : default_date.d;
m=mm ? mm : default_date.m;
y=yy ? yy : default_date.y;
}

void f()
{
    Date::set_default(4,5,1945);
}

Date Date::default_date(16,12,1770);


void Date::set_default(int d,int m, int y)
{
    Date::default_date=Date(d,m,y);
}


int main(){


Date ob(5,5);
cout<<ob.day()<<endl<<ob.month()<<endl<<ob.year();


return 0;
}

The output of the code was 5 5 1770

He states that the default_date must be defined somewhere, as it is done between f() and set default. While we are at it can somebody tell me why is it with two Date before :: and not one?

OK default_date can be constructed with the constructor provided since all arguments are supplied.

Now i tried removing the year from the list of arguments for default_date:

Date Date::default_date(16,12);

and the program compiled fine with output 5,5,0. This means that when in the constructor, since the last argument is not provided yy is 0 and default_date.y should get default_date.y and it turns out that it is 0. To me this only makes sense if the members of default_date are set to 0 when default_date is declared, and changed when default_date is defined. If that is true why then must we define default_date, why cant we leave it be 0,0,0?

هل كانت مفيدة؟

المحلول

default_date is a Date object which is a static member of the Date class. That's why there are two Dates in front of its definition:

        qualified id
     vvvvvvvvvvvvvvvvvv
Date Date::default_date(16,12,1770);
^^^^ ^^^^
 |     default_date is a member of Date
 |
The type of default_date

By removing the year from the definition, the Date constructor will end up assigning default_date.y to itself. Why does this have value 0? Shouldn't it be uninitialized? Actually, static objects will be zero-initialized before any other initialization takes place. So the members of default_date will all be set to 0 before the constructor runs.

نصائح أخرى

// This is simply how static variables are initialized
// type classname::attributename followed by either = or constructor invocation
Date Date::default_date(16,12,1770);

// two ways of initializing an int in c++
int i = 0;
int i(0);

What you see in the example is similar to the second form above, except that it is within a class so you need the scope to be included.

If that is true why then must we define default_date, why cant we leave it be 0,0,0?

If you are in a large scale app with multiple compilation units, you will get linker errors if you don't create a static initializer in the cpp file for the corresponding class declaration. In your example everything is within one file, and it appears that you don't need to do it. I deleted that line, and it still compiled fine with VS2010.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top