C++中聚合类型(aggreate)和 palin old data

    plain old data常常简写为POD, 它是聚合类型(aggregate)的一种特例。 

    因此在讲解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-defined 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 (){} // 用户定义的额析构函数

};

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值