POD型别

55 篇文章 0 订阅

POD全称Plain Old Data。通俗的讲,一个类或结构体通过二进制拷贝后还能保持其数据不变,那么它就是一个POD类型。

平凡的定义:

1. 有平凡的构造函数;

2. 有平凡的拷贝构造函数;

3. 有平凡的移动构造函数;

4. 有平凡的拷贝赋值运算符;

5. 有平凡的移动赋值运算符;

6. 有平凡的析构函数;

7. 不能包含虚函数;

8. 不能包含虚基类。

#include "stdafx.h"

#include <iostream>

using namespace std;

class A { A(){} };

class B { B(B&){} };

class C { C(C&&){} };

class D { D operator=(D&){} };

class E { E operator=(E&&){} };

class F { ~F(){} };

class G { virtual void foo() = 0; };

class H : G {};

class I {};

int _tmain(int argc, _TCHAR* argv[])

{

    std::cout << std::is_trivial<A>::value << std::endl;  // 有不平凡的构造函数

    std::cout << std::is_trivial<B>::value << std::endl;  // 有不平凡的拷贝构造函数

    std::cout << std::is_trivial<C>::value << std::endl;  // 有不平凡的拷贝赋值运算符

    std::cout << std::is_trivial<D>::value << std::endl;  // 有不平凡的拷贝赋值运算符

    std::cout << std::is_trivial<E>::value << std::endl;  // 有不平凡的移动赋值运算符

    std::cout << std::is_trivial<F>::value << std::endl;  // 有不平凡的析构函数

    std::cout << std::is_trivial<G>::value << std::endl;  // 有虚函数

    std::cout << std::is_trivial<H>::value << std::endl;  // 有虚基类

    std::cout << std::is_trivial<I>::value << std::endl;  // 平凡的类

    system("pause");

    return 0;

}

标准布局的定义:

1. 所有非静态成员有相同的访问权限;

2. 继承树中最多只能有一个类有非静态数据成员;

3. 子类的第一个非静态成员不可以是基类类型;

4. 没有虚函数;

5. 没有虚基类;

6. 所有非静态成员都符合标准布局类型。

#include "stdafx.h"

#include <iostream>

using namespace std;

class A

{

private:

    int a;

public:

    int b;

};

class B1

{

    static int x1;

};

class B2

{

    int x2;

};

class B : B1, B2

{

    int x;

};

class C1 {};

class C : C1

{

    C1 c;

};

class D { virtual void foo() = 0; };

class E : D {};

class F { A x; };

int _tmain(int argc, _TCHAR* argv[])

{

    std::cout << std::is_standard_layout<A>::value << std::endl;  // 违反定义1。成员a和b具有不同的访问权限

    std::cout << std::is_standard_layout<B>::value << std::endl;  // 违反定义2。继承树有两个(含)以上的类有非静态成员

    std::cout << std::is_standard_layout<C>::value << std::endl;  // 违反定义3。第一个非静态成员是基类类型

    std::cout << std::is_standard_layout<D>::value << std::endl;  // 违反定义4。有虚函数

    std::cout << std::is_standard_layout<E>::value << std::endl;  // 违反定义5。有虚基类

    std::cout << std::is_standard_layout<F>::value << std::endl;  // 违反定义6。非静态成员x不符合标准布局类型

    system("pause");

    return 0;

}

当一个数据类型满足了“平凡的定义”和“标准布局”,我们就认为它是一个POD数据——可以通过std::is_pod来判断一个类型是否为POD类型。

如文章开头说的,一个POD类型是可以进行二进制拷贝的,看看下面的例子——

#include "stdafx.h"

#include <iostream>

#include <Windows.h>

using namespace std;

class A

{

public:

    int x;

    double y;

};

int _tmain(int argc, _TCHAR* argv[])

{

    if (std::is_pod<A>::value)

    {

        std::cout << "before" << std::endl;

        A a;

        a.x = 8;

        a.y = 10.5;

        std::cout << a.x << std::endl;

        std::cout << a.y << std::endl;

        size_t size = sizeof(a);

        char *p = new char[size];

        memcpy(p, &a, size);

        A *pA = (A*)p;

        std::cout << "after" << std::endl;

        std::cout << pA->x << std::endl;

        std::cout << pA->y << std::endl;

        ete p;

    }

    system("pause");

    return 0;

}

可以看到,对一个POD类型进行二进制拷贝后,数据都成功地迁移过来了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值