简体   繁体   English

如何初始化属于类的成员并且与它们所在的类具有相同类型的静态对象?

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

Bjarne Strousrup in The C++ Programming Language illustrates the use of static members of a class with code similar to this: C ++编程语言中的Bjarne Strousrup演示了如何使用类似于以下代码的类的静态成员:

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 代码的输出是5 5 1770

He states that the default_date must be defined somewhere, as it is done between f() and set default. 他指出default_date必须在某处定义,因为它是在f()和set default之间完成的。 While we are at it can somebody tell me why is it with two Date before :: and not one? 当我们在这里时,有人可以告诉我为什么在::之前有两个Date而没有一个吗?

OK default_date can be constructed with the constructor provided since all arguments are supplied. 因为提供了所有参数,所以可以使用提供的构造函数来构造default_date。

Now i tried removing the year from the list of arguments for default_date: 现在,我尝试从default_date的参数列表中删除年份:

Date Date::default_date(16,12);

and the program compiled fine with output 5,5,0. 并且程序编译良好,输出为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. 这意味着在构造函数中,由于未提供最后一个参数,所以yy为0,default_date.y应该为default_date.y,结果为0。对我来说,只有将default_date的成员设置为声明default_date时为0,定义default_date时更改。 If that is true why then must we define default_date, why cant we leave it be 0,0,0? 如果是这样,为什么我们必须定义default_date,为什么不能将其保留为0,0,0?

default_date is a Date object which is a static member of the Date class. default_date是一个Date对象,它是Date类的静态成员。 That's why there are two Date s in front of its definition: 这就是为什么在其定义前面有两个Date的原因:

        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. 通过从定义中删除年份, Date构造函数将最终为其分配default_date.y Why does this have value 0? 为什么它的值为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. 因此,在构造函数运行之前, default_date的成员都将设置为0。

// 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? 如果是这样,为什么我们必须定义default_date,为什么不能将其保留为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. 如果您在具有多个编译单元的大型应用程序中,如果未在cpp文件中为相应的类声明创建静态初始化器,则将出现链接器错误。 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. 我删除了该行,并且在VS2010中仍然可以正常编译。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM