C++中聚合类型(aggreate)和 palin old data
2014-10-20 17:19
363 查看
plain old data常常简写为POD, 它是聚合类型(aggregate)的一种特例。
因此在讲解POD之前,我们先了解一下什么是聚合类型。
An aggregate is an array or a class(claluse 9) with no user-declaread constructors(12.1), no private or protected non-static data members(clause 11),
no base classes(clause10), no viutural functions(10.3).
简单的说,聚合类型分为数组和类。类必须满足以下情况:
a、没有用户声明的构造函数(no user-declaread constructors)
b、没有私有或保护的非静态数据成员( no private or protected non-static data members)
c、 没有基类( no base classes)
d、没有虚函数(no virtual functions)
违反其中任何一条约束,都不是聚合类型。
示例:
class A { // ok, 类A没有基类
private:
static int x; // ok, 'x' 是静态数据成员
public:
void fun() { return; } // ok, fun() 是普通的成员函数
};
A 就是一个聚合类型。
class B {
B(); //no, 用户声明的构造函数
};
class C {
int x; // no, 私有的非静态数据成员
protected:
int y; // no, 保护的非静态数据成员
};
class D : A { // no, 有基类(A)
};
class E {
virtual void fun() { return; } // no, 虚函数
};
以上是C++98/03的定义,在C++11和C++1y(即将发布的C++14的代号)中又有所修改。具体如下:
C++11中的定义:
An aggregate is an array or a class (Clause 9) with
no user-provided constructors (12.1),
no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause11), no base classes (Clause10), and no virtual functions (10.3).
C++1y中的定义:
An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no
brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause11), no base classes (Clause10), and no virtual functions (10.3).
特别申明此处的user-provided constructors 和 C++98/03中的user-declared constructors是不一样。
copy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union 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. A POD class is a class that is either a POD-struct or a POD-union.
POD是一个聚合类型,但是聚合类型的非静态数据成员必须满足以下条件:
a、没有非POD结构体的数据成员(no non-static data members of type non-POD-struct)
b、没有非POD联合体的数据成员(no non-static data members of type non POD-union)
c、没有引用(no non-static data members of reference)
成员函数满足以下条件:
d、没有用户定义的拷贝赋值操作符(no user-defined copy assignment operator)
e、 没有用户定义的析构由函数(no user-defined destructor)
特别申明,如果包含数组,那么数组元素类型也必须是non-POD-struct, non POD-union型。
示例:
struct POD {
int x; // 公有的数据成员, public
char y; // 普通成员函数
static std::vector<char> v; // 静态数据成员
}';
struct Aggregate_NOT_POD1 {
int x;
~Aggregate_NOT_POD (){} // 用户定义的额析构函数
};
struct Aggregate_NOT_POD2 {
Aggregate_NOT_POD1 arr_Non_POD[3]; // 数组元素类型不是POD型
};
int x;
~Aggregate_NOT_POD (){} // 用户定义的额析构函数
};
因此在讲解POD之前,我们先了解一下什么是聚合类型。
1. 聚合类型(aggregate)
C++98/03中的定义:An aggregate is an array or a class(claluse 9) with no user-declaread constructors(12.1), no private or protected non-static data members(clause 11),
no base classes(clause10), no viutural functions(10.3).
简单的说,聚合类型分为数组和类。类必须满足以下情况:
a、没有用户声明的构造函数(no user-declaread constructors)
b、没有私有或保护的非静态数据成员( no private or protected non-static data members)
c、 没有基类( no base classes)
d、没有虚函数(no virtual functions)
违反其中任何一条约束,都不是聚合类型。
示例:
class A { // ok, 类A没有基类
private:
static int x; // ok, 'x' 是静态数据成员
public:
void fun() { return; } // ok, fun() 是普通的成员函数
};
A 就是一个聚合类型。
class B {
B(); //no, 用户声明的构造函数
};
class C {
int x; // no, 私有的非静态数据成员
protected:
int y; // no, 保护的非静态数据成员
};
class D : A { // no, 有基类(A)
};
class E {
virtual void fun() { return; } // no, 虚函数
};
以上是C++98/03的定义,在C++11和C++1y(即将发布的C++14的代号)中又有所修改。具体如下:
C++11中的定义:
An aggregate is an array or a class (Clause 9) with
no user-provided constructors (12.1),
no brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause11), no base classes (Clause10), and no virtual functions (10.3).
C++1y中的定义:
An aggregate is an array or a class (Clause 9) with no user-provided constructors (12.1), no
brace-or-equal-initializers for non-static data members (9.2), no private or protected non-static data members (Clause11), no base classes (Clause10), and no virtual functions (10.3).
特别申明此处的user-provided constructors 和 C++98/03中的user-declared constructors是不一样。
2. POD
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-definedcopy assignment operator and no user-defined destructor. Similarly, a POD-union is an aggregate union 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. A POD class is a class that is either a POD-struct or a POD-union.
POD是一个聚合类型,但是聚合类型的非静态数据成员必须满足以下条件:
a、没有非POD结构体的数据成员(no non-static data members of type non-POD-struct)
b、没有非POD联合体的数据成员(no non-static data members of type non POD-union)
c、没有引用(no non-static data members of reference)
成员函数满足以下条件:
d、没有用户定义的拷贝赋值操作符(no user-defined copy assignment operator)
e、 没有用户定义的析构由函数(no user-defined destructor)
特别申明,如果包含数组,那么数组元素类型也必须是non-POD-struct, non POD-union型。
示例:
struct POD {
int x; // 公有的数据成员, public
char y; // 普通成员函数
static std::vector<char> v; // 静态数据成员
}';
struct Aggregate_NOT_POD1 {
int x;
~Aggregate_NOT_POD (){} // 用户定义的额析构函数
};
struct Aggregate_NOT_POD2 {
Aggregate_NOT_POD1 arr_Non_POD[3]; // 数组元素类型不是POD型
};
int x;
~Aggregate_NOT_POD (){} // 用户定义的额析构函数
};
相关文章推荐
- C++ POD(Plain Old Data)类型
- C++ POD(Plain Old Data)类型
- C++ POD(Plain Old Data)类型
- C++ POD(Plain Old Data)类型
- C++ POD(Plain Old Data)类型
- C++ POD(Plain Old Data)类型
- C++不同数据类型的取值范围(Data Type Ranges )
- C++ POD(Plain Old Data)类型
- 1.2 C++变量和数据类型 (Variables and Data types )
- Lengths of Primitive Java and C++ Data Types on Linux and Windows 32-bit/64-bit (Linux和Windows上Java和C++基本数据类型的长度对比32位/64位)
- 在Elasticsearch中对 text 类型的字段进行聚合异常Fielddata is disabled,Set fielddata=true
- Lua收到C++传入的参数类型有一种userdata
- C++中的Plain Old Data(POD)
- C++ trivial和non-trivial及POD类型POD(Plain Old Data)
- symbian c++ 学习 2 数据类型及代码规范
- C++风格的类型转换的用法
- Dev-C++下关于几种int类型格式符的实验
- 明确C++风格的类型转换的用法
- Dev-C++下关于char类型的实验
- C++常类型(const)