简体   繁体   English

c++0x 中访问说明符和使用 POD 类型的初始化列表之间的关系

[英]relation between access specifiers and using initializer lists for POD types in c++0x

take two following classes:参加以下两门课程:

class Test1{
 public:
  Test1()=default;
  Test1(char in1,char in2):char1(in1),char2(in2){}
  char char1;
  char char2;
};
class Test2{
 public:
  Test2()=default;
  Test2(char in1,char in2):char1(in1),char2(in2){}
 private:
  char char1;
  char char2;
};

I know in c++0x both of these classes are considered as POD types and we can initialize objects of them using initializer lists as below:我知道在 c++0x 中,这两个类都被视为 POD 类型,我们可以使用初始化列表初始化它们的对象,如下所示:

Test1 obj1={'a','b'};//valid in c++0x
Test2 obj2={'a','b'};//valid in c++0x

But I wonder what the technical reason is that when we have different access specifiers in a class like below, it's not possible to use initializer list for initializing objects of that class and that class is not considered as a POD type ?但我想知道技术原因是什么,当我们在如下类中有不同的访问说明符时,不可能使用初始化列表来初始化该类的对象,并且该类不被视为 POD 类型?

class Test{
 public:
  Test()=default;
  Test(char in1,char in2):char1(in1),char2(in2){}
  char char1;
 private:
  char char2;
};
Test obj={'a','b'};//invalid in c++0x

In case you don't know definition of PODs in c++0x:如果您不知道 C++0x 中 POD 的定义:
A class/struct is considered a POD if it is trivial, standard-layout, and if all of its non-static members are PODs.如果一个类/结构是简单的、标准布局的,并且它的所有非静态成员都是 POD,那么它就被认为是 POD。

A trivial class or struct is defined as one that:一个平凡的类或结构被定义为:

  1. Has a trivial default constructor.有一个简单的默认构造函数。 This may use the default constructor syntax (SomeConstructor() = default;).这可以使用默认构造函数语法(SomeConstructor() = default;)。
  2. Has a trivial copy constructor, which may use the default syntax.有一个简单的复制构造函数,它可以使用默认语法。
  3. Has a trivial copy assignment operator, which may use the default syntax.有一个简单的复制赋值运算符,它可以使用默认语法。
  4. Has a trivial destructor, which must not be virtual.有一个微不足道的析构函数,它不能是虚拟的。

A standard-layout class or struct is defined as one that:标准布局类或结构被定义为:

  1. Has only non-static data members that are of standard-layout type只有标准布局类型的非静态数据成员
  2. Has the same access control (public, private, protected) for all non-static members对所有非静态成员具有相同的访问控制(公共、私有、受保护)
  3. Has no virtual functions没有虚函数
  4. Has no virtual base classes没有虚拟基类
  5. Has only base classes that are of standard-layout type只有标准布局类型的基类
  6. Has no base classes of the same type as the first defined non-static member没有与第一个定义的非静态成员相同类型的基类
  7. Either has no base classes with non-static members, or has no non-static data members in the most derived class and at most one base class with non-static members.要么没有具有非静态成员的基类,要么在最派生的类中没有非静态数据成员,而至多一个具有非静态成员的基类。 In essence, there may be only one class in this class's hierarchy that has non-static members.本质上,此类的层次结构中可能只有一个具有非静态成员的类。

In case you don't know what a trivial constructor or operator is:如果您不知道什么是普通构造函数或运算符:
Compiler generates a trivial one of each of following items for a class, in case it isn't user-declared: Copy constructor, destructor and copy assignment operator.编译器为类生成以下各项中的一个,以防它不是用户声明的:复制构造函数、析构函数和复制赋值运算符。
And also if there's no user-declared constructor for a class, a trivial default constructor is generated for that class, in case there are any user-declared constructors you can use the syntax(SomeConstructor() = default;) to make your own trivial default constructor.而且,如果类没有用户声明的构造函数,则会为该类生成一个简单的默认构造函数,以防有任何用户声明的构造函数,您可以使用语法(SomeConstructor() = default;) 来制作自己的简单构造函数默认构造函数。

The "technical" reason is due to the following: “技术”原因是由于以下原因:

Nonstatic data members of a (non-union) class with the same access control are allocated so that later members have higher addresses within a class object.分配具有相同访问控制的(非联合)类的非静态数据成员,以便后面的成员在类对象中具有更高的地址。 The order of allocation of non-static data members with different access control is unspecified (C++0x §9.2/12).具有不同访问控制的非静态数据成员的分配顺序未指定(C++0x §9.2/12)。

So long as all the nonstatic data members have the same access control, their order is well-specified;只要所有非静态数据成员都具有相同的访问控制,它们的顺序就是明确指定的; otherwise their order is unspecified.否则它们的顺序是未指定的。

class Test{
 public:
  Test()=default;
  Test(char in1,char in2):char1(in1),char2(in2){}
  char char1;
 private:
  char char2;
};

considering above class following syntax is valid in c++0x:考虑到上面的类,以下语法在 c++0x 中是有效的:

Test obj={'a','b'};//valid in c++0x

The final proposal is here .最终提案在这里

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

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