gpt4 book ai didi

c++ - POD 类中的嵌套枚举是否使其不是 POD?

转载 作者:搜寻专家 更新时间:2023-10-31 00:15:23 26 4
gpt4 key购买 nike

根据定义here , POD 是一个简单的类,没有用户定义的构造函数、非静态成员,并且只包含简单的数据类型。

问题是,下面这两个类是否等同于 POD 类型(就内存占用而言):

class pod
{
public:
int x;
double y;
};

class pod1
{
public:
int x;
double y;

enum POD_TYPE
{
POD1 = 0,
POD = 1
};
};

换句话说,将enum添加到类中只会影响enum的作用域解析,而不会影响类本身的属性吗?通过观察,类似乎仍然是pod,但我想根据标准进行确认。

最佳答案

是的,那个类型仍然是 POD。定义由 C++11 9/10 给出:

A POD struct is a non-union class that is both a trivial class and a standard-layout class, and has no non-static data members of type non-POD struct, non-POD union (or array of such types).

Trivial 意味着它在创建、销毁或复制对象时不会做任何有趣的事情。 标准布局 意味着它不会对数据成员的布局做任何有趣的事情:没有多态性,也没有限制您可以使用访问说明符和继承做什么。如果您需要更多详细信息,这些术语在 C++11 9/6 和 9/7 中有完整定义。

嵌套类型(例如您的枚举)、静态数据成员和非虚拟成员函数(除了构造函数等会使它变得不平凡)不会影响任何这些东西,所以它仍然是 POD。

更新:既然你说你对历史定义感兴趣,C++03 定义:

9/4 A POD-struct is an aggregate class that has no non-static members of type non-POD-struct, non-POD-union (or array of such types), and has no user-defined copy assignment operator and no user-defined destructor"

8.5.1/1 An aggregate is an array or class with no user-declared constructors, no private or protected non-static data members, no base classes and no virtual functions.

所以有更多的限制;但仍然允许嵌套类型。我没有 C++98 的拷贝,但我确信它与 C++03 相同。

关于c++ - POD 类中的嵌套枚举是否使其不是 POD?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19590008/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com