gpt4 book ai didi

c++ - 具有私有(private)成员的普通旧数据类型?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:55:46 25 4
gpt4 key购买 nike

Demo是C++03中的POD类型吗?

struct Demo
{
private:
int x;
int y;
};

C++03,§9p4:

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

看完Steve Jessop's post ,我相信 Demo 是非 POD,因为成员是私有(private)的。然而,标准没有说明 POD 类型和访问修饰符之间的关系。

在 C++0x 中,Demo 是 POD,因为 §9p9 (n3126) 说:

A POD struct is a 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).

Demo 很简单1 也是一个标准布局类,因此它是一个 POD。我的解释正确吗?

1 普通类是具有普通默认构造函数 (12.1) 且可普通复制的类。 [9p5, n3126]

最佳答案

在 C++03 中,它绝对不是 POD。根据 §9/4,“POD-struct 是一个聚合类......”,根据 §8.5.1/1:

An aggregate is an array or a class (clause 9) with no user-declared constructors (12.1), no private or protected non-static data members (clause 11), no base classes (clause 10), and no virtual functions (10.3)."

在 C++0x 下,至少从 N3090/3092 开始,我相信它一个 POD。这些仅要求所有非静态成员具有相同的访问权限,而不是访问必须是公共(public)的。这是为了解决我相信我是第一个指出的问题——在 C++98/03 中,空洞的访问说明符会导致问题:

struct x { 
int a;
public:
int b;
public:
int c;
};

这符合 POD 结构的要求——但是由于中间访问说明符,标准仍然允许交换 bc 的相对位置.因此,作为 POD 结构不提供旨在确保与 C 结构兼容的布局保证(对于明显的示例)。

关于c++ - 具有私有(private)成员的普通旧数据类型?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3952020/

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