什么是聚合和 POD,它们如何/为什么特别?

What are Aggregates and PODs and how/why are they special?(什么是聚合和 POD,它们如何/为什么特别?)

本文介绍了什么是聚合和 POD,它们如何/为什么特别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此常见问题解答是关于聚合和POD的,涵盖以下材料:

This FAQ is about Aggregates and PODs and covers the following material:

  • 什么是聚合?
  • 什么是POD(普通旧数据)?
  • 它们有什么关系?
  • 它们有什么特别之处,为什么特别?
  • C++11 有哪些变化?

推荐答案

如何阅读:

这篇文章比较长.如果您想了解聚合和 POD(普通旧数据),请花时间阅读.如果您只对聚合感兴趣,请阅读第一部分.如果您只对 POD 感兴趣,那么您必须首先阅读聚合的定义、含义和示例,然后可能跳到 POD,但我仍然建议您完整阅读第一部分.聚合的概念对于定义 POD 至关重要.如果您发现任何错误(即使是很小的错误,包括语法、文体、格式、语法等),请发表评论,我会编辑.

How to read:

This article is rather long. If you want to know about both aggregates and PODs (Plain Old Data) take time and read it. If you are interested just in aggregates, read only the first part. If you are interested only in PODs then you must first read the definition, implications, and examples of aggregates and then you may jump to PODs but I would still recommend reading the first part in its entirety. The notion of aggregates is essential for defining PODs. If you find any errors (even minor, including grammar, stylistics, formatting, syntax, etc.) please leave a comment, I'll edit.

这个答案适用于 C++03.有关其他 C++ 标准,请参阅:

This answer applies to C++03. For other C++ standards see:

  • C++11 更改
  • C++14 更改
  • C++17 更改
  • C++20 更改

来自 C++ 标准的正式定义(C++03 8.5.1 §1):

Formal definition from the C++ standard (C++03 8.5.1 §1):

聚合是没有用户声明的数组或类(第 9 条)构造函数(12.1),没有私有或受保护的非静态数据成员(第 11 条),没有基类(第 10 条),也没有虚函数(10.3).

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++ 中,术语 class 指代所有的类、结构和联合.因此,一个类(或结构或联合)是一个聚合当且仅当它满足上述定义的标准.这些标准意味着什么?

So, OK, let's parse this definition. First of all, any array is an aggregate. A class can also be an aggregate if… wait! nothing is said about structs or unions, can't they be aggregates? Yes, they can. In C++, the term class refers to all classes, structs, and unions. So, a class (or struct, or union) is an aggregate if and only if it satisfies the criteria from the above definitions. What do these criteria imply?

  • 这并不意味着聚合类不能有构造函数,事实上它可以有默认构造函数和/或复制构造函数,只要它们是由编译器隐式声明的,而不是由用户显式声明的

  • This does not mean an aggregate class cannot have constructors, in fact it can have a default constructor and/or a copy constructor as long as they are implicitly declared by the compiler, and not explicitly by the user

没有私有或受保护的非静态数据成员.您可以拥有任意数量的私有和受保护的成员函数(但不是构造函数)以及许多私有或受保护的静态数据成员和成员函数,并且不违反聚合类规则

No private or protected non-static data members. You can have as many private and protected member functions (but not constructors) as well as as many private or protected static data members and member functions as you like and not violate the rules for aggregate classes

聚合类可以有一个用户声明/用户定义的复制赋值运算符和/或析构函数

An aggregate class can have a user-declared/user-defined copy-assignment operator and/or destructor

一个数组是一个聚合,即使它是一个非聚合类类型的数组.

An array is an aggregate even if it is an array of non-aggregate class type.

现在让我们看一些例子:

Now let's look at some examples:

class NotAggregate1
{
  virtual void f() {} //remember? no virtual functions
};

class NotAggregate2
{
  int x; //x is private by default and non-static 
};

class NotAggregate3
{
public:
  NotAggregate3(int) {} //oops, user-defined constructor
};

class Aggregate1
{
public:
  NotAggregate1 member1;   //ok, public member
  Aggregate1& operator=(Aggregate1 const & rhs) {/* *

本文标题为:什么是聚合和 POD,它们如何/为什么特别?

基础教程推荐