POD stands for Plain Old Data,个人实践总结,表现得如同C-struct和表示得如同RAW_data,纯粹的数据;
#include<iostream>#include<cstdio>class Cat{public: int v1; int v2; //生命周期不由构造函数和析构函数控制,因此:Cat(),不可以有;~Cat(),不可以有 //虚函数表和虚指针都需要额外的空间,因此virtual function 不可以有。 //不可以有继承其它类,因为继承的类可能需要不可控制的额外的空间。 //不可以有权限控制,PRotect/private成员变量不可以有。 //默认的复制构造很重要,自定义的Cat(Cat& other),不可以有,请看例子。 //可以有普通成员函数,它们与占用空间无关。 void customFunction() {} //可以有POD类型的静态成员,因为静态成员实际上不属于类,不占用类空间,只是作用域在类中。 static Cat c; static int n;//int ,float,char等基础类型都是POD //可以有Operator= Cat& operator=(Cat& other) {};};int main(){ Cat c1; Cat c2; int a[2] = { 12,88 }; if ((int)&c1 == (int)&(c1.v1)) { std::cout << "same address" << std::endl; memcpy(&c1, &a, sizeof(int) * 2); std::cout << c1.v1 << std::endl; std::cout << c1.v2 << std::endl; memcpy(&c2, &c1, sizeof(Cat)); std::cout << c2.v1 << std::endl; std::cout << c2.v2 << std::endl; } int int_group[10] = {};//对每个成员调用默认:int() Cat cat_group_a[10] = {};//用到了默认的构造函数,对每个成员类初始化。 Cat cat_group_b[10] = {1};//对第一个成员类的第一个成员变量初始化,用到了默认的复制构造函数。 std::cout << cat_group_b[0].v1 << std::endl; return 0;}参考文献 http://stackoverflow.com/questions/4178175/what-are-aggregates-and-pods-and-how-why-are-they-special http://stackoverflow.com/questions/146452/what-are-pod-types-in-c
新闻热点
疑难解答
图片精选