加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C struct默认构造函数行为

发布时间:2020-12-16 09:52:02 所属栏目:百科 来源:网络整理
导读:据我所知,如果程序员没有实现任何构造函数,编译器会自动生成不执行任何操作的默认构造函数. 我认为它也适用于struct. 我用它来继承. struct Parent { int a;};struct Child : Parent { int b; Child () { printf("child constructorn"); }}int main () { Ch
据我所知,如果程序员没有实现任何构造函数,编译器会自动生成不执行任何操作的默认构造函数.
我认为它也适用于struct.

我用它来继承.

struct Parent {
    int a;
};

struct Child : Parent {
    int b;
    Child () {
        printf("child constructorn");
    }
}

int main () {
    Child c;
    printf("%dn",c.a);        // c.a = dummy value
    printf("%dn",c.b);        // c.b = 0
    return 0;
}

在上面的代码中,c.a是虚拟值.但是,当我更改Child构造函数时,它为零.
唯一的区别是Child在其构造函数中显式调用父的构造函数.

struct Parent {
    int a;
};

struct Child : Parent {
    int b;
    Child () : Parent () {
        printf("child constructorn");
    }
}

int main () {
    Child c;
    printf("%dn",c.a);        // c.a = 0
    printf("%dn",c.b);        // c.b = 0
    return 0;
}

怎么了?
我花了一些时间但找不到任何理由.
我使用的编译器是VS2008.
先感谢您.

解决方法

Parent是POD类型(普通旧数据类型).它没有构造函数或析构函数(除了隐式的,也是微不足道的),复杂的成员等.它只是一个带有标量或其他POD类型成员的结构.当你这样做:

struct Child : Parent 
{
    int b;
    Child () {
        printf("child constructorn");
    }
}

你只是构建子对象.没有相关的父母建设.基类型经过默认初始化:

C++11 § 8.5,p11

If no initializer is specified for an object,the object is default-initialized; if no initialization is performed,an object with automatic or dynamic storage duration has indeterminate value. [ Note: Objects with static or thread storage duration are zero-initialized,see 3.6.2.

对于您的POD类型,这意味着:

C++11 § 8.5,p6

To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9),the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

  • if T is an array type,each element is default-initialized;

  • otherwise,no initialization is performed.

但是当你这样做时:

struct Child : Parent 
{
    int b;
    Child () : Parent () {
        printf("child constructorn");
    }
}

发生了不同的事情:您正在调用Parent基类型的值初始化.因为 ().

C++11 § 8.5,p10

An object whose initializer is an empty set of parentheses,i.e.,(),shall be value-initialized.

对于POD类型(其中Parent为1),值初始化最终将对成员进行零初始化.

C++11 § 8.5,p7

To value-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9) with a user-provided constructor (12.1),then the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);

  • if T is a (possibly cv-qualified) non-union class type without a user-provided constructor,then the object is zero-initialized and,if T’s implicitly-declared default constructor is non-trivial,that constructor is called.

  • if T is an array type,then each element is value-initialized;

  • otherwise,the object is zero-initialized.

因此,在第二种情况下a为零,但在第一种情况下不是. (好吧,它在第一个中可能为零,你真的不能说;它的值是不确定的,直到你给它分配东西).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读