空初始化的构造函数初始化列表 [英] Constructor initialization list with empty initialization

查看:164
本文介绍了空初始化的构造函数初始化列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

_currentHandle()的含义是什么?

What does _currentHandle() mean below?

template<class SpiHandleT>
class SpiHandleIterator : public ISpiHandleIterator<SpiHandleT>
{
public:
    SpiHandleIterator() : _currentHandle()
    {
    }
    ...
protected:
    SpiHandleT _currentHandle;
};


推荐答案

这就是 value-initialization 。根据C ++ 03标准的§8.5/ 7:

This is known as value-initialization. From the C++03 standard, §8.5/7:


一个对象,其初始化程序是一组空括号,即(),应为value

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

然后从§8.5/ 5开始:

And from §8.5/5:


值初始化类型为 T 的对象的意思是:

To value-initialize an object of type T means:


  • 如果 T 是具有用户声明的构造函数的类类型,然后调用 T 的默认构造函数(并且初始化是错误的-如果 T 没有可访问的默认构造函数,则形成);

  • 如果 T 是没有用户声明的构造函数的非工会类类型,则 T 的每个非静态数据成员和基类组件都将值初始化;

  • 如果 T 是数组类型,则每个元素都将值初始化;

  • 否则,对象是零初始化

  • if T is a class type with a user-declared constructor, 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 non-union class type without a user-declared constructor, then every non-static data member and base-class component of T is value-initialized;
  • if T is an array type, then each element is value-initialized;
  • otherwise, the object is zero-initialized

零初始化类型为 T 表示:


  • 如果 T 是标量类型,则将对象设置为 0 的值(零)转换为 T ;

  • 如果 T 是非工会类类型,每个非静态数据成员和每个基类子对象都被零初始化;

  • 如果 T 是联合类型,则对象的首先命名的数据成员)被零初始化;

  • 如果 T 是数组类型,则每个元素都被零初始化;
  • li>
  • 如果 T 是引用类型,则不执行初始化。

  • if T is a scalar type, the object is set to the value of 0 (zero) converted to T;
  • if T is a non-union class type, each nonstatic data member and each base-class subobject is zero-initialized;
  • if T is a union type, the object’s first named data member) is zero-initialized;
  • if T is an array type, each element is zero-initialized;
  • if T is a reference type, no initialization is performed.

因此,在您的情况下,它取决于 SpiHandleT 的定义:

So in your case, it depends on the definition of SpiHandleT:


  • 如果是标量,则将被初始化为零

  • 如果它是类类型,而没有用户声明的构造函数,则其子对象将是(递归)值初始化

  • 如果它是带有用户声明的构造函数的 类类型,则它将是默认构造的

  • If it's a scalar, it will be zero-initialized
  • If it's a class type without a user-declared constructor, its subobjects will be (recursively) value-initialized
  • If it's a class type with a user-declared constructor, it will be default-constructed

这篇关于空初始化的构造函数初始化列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆