什么是POD

 C++语言中经常会提到POD,就是plain old data的缩写。那么究竟什么是POD呢

 维基百科上的解释:

http://en.wikipedia.org/wiki/Plain_Old_Data_Structures

A plain old data structure (POD) is a data structure that is represented only as passive collections of field values, without using encapsulation or other object-oriented features.

Plain old data structures are appropriate when there is a part of a system where it should be clearly indicated that the detailed logic for data manipulation and integrity are elsewhere. PODs are often found at the boundaries of a system, where information is being moved to and from other systems or persistent storage and the business logic that is found in other parts of the system is not relevant. For example, PODs would be convenient for representing the field values of objects that are being constructed from external data, in a part of the system where the semantic checks and interpretations needed for valid objects have not yet been applied.

A POD type in C++ is defined as either a scalar type or a POD class. A POD class has no user-defined copy assignment operator, no user-defined destructor, and no non-static data members that are not themselves PODs. Moreover, a POD class must be an aggregate, meaning it has no user-declared constructors, no private nor protected non-static data, no base classes and no virtual functions. The standard includes statements about how PODs must behave in C++.

POD类型不能包含用户定义的拷贝构造函数,不能包含自定义的析构函数,不能包含非静态的非POD类型数据成员。而且,POD类型只是一个集合,即它没有自定义的构造函数,没有私有的或受保护的非静态数据,没有基类和虚函数。

In certain contexts, C++ allows only POD types to be used. For example, a union in C++ cannot contain a class that has virtual functions or nontrivial constructors or destructors. This restriction is imposed because the compiler cannot know which constructor or destructor should be called for a union. POD types can also be used for interfacing with C, which supports only PODs.

In Java, some developers consider that the POD concept corresponds to a class with public data members and no methods (Sun Code Conventions 10.1),[citation needed] i.e., a data transfer object. Others would also include POJOs (a class that has methods but only getters and setters, with no logic) to be a POD. Java Beans fall under the POD concept if they do not use event handling and do not implement additional methods beyond getters and setters.[citation needed]

Other structured data representations such as XML can also be used as PODs if no significant semantic restrictions are used.

 

stackoverflow上的一个解答:

http://stackoverflow.com/questions/146452/what-are-pod-types-in-c

A POD is a type (including classes) where the C++ compiler guarantees that there will be no "magic" going on in the structure: for example hidden pointers to vtables, offsets that get applied to the address when it is cast to other types (at least if the target's POD too), constructors, or destructors. Roughly speaking, a type is a POD when the only things in it are built-in types and combinations of them. The result is something that "acts like" a C type.

Less informally:

  • int, char, wchar_t, bool, float, double are PODs, as are long/short and signed/unsigned versions of them.
  • pointers (including pointer-to-function and pointer-to-member) are PODs,
  • enums are PODs
  • a const or volatile POD is a POD.
  • a class, struct or union of PODs is a POD provided that all members are public, and it has no base class and no constructors, destructors, or virtual methods. Static members don't stop something being a POD under this rule.
  • Wikipedia is wrong to say that a POD cannot have members of type pointer-to-member. Or rather, it's correct for the C++98 wording, but TC1 made explicit that pointers-to-member are POD.

Here's what the C++ standard says:

3.9(10): "Arithmetic types (3.9.1), enumeration types, pointer types, and pointer to member types (3.9.2) and cv-qualified versions of these types (3.9.3) are collectively caller scalar types. Scalar types, POD-struct types, POD-union types (clause 9), arrays of such types and cv-qualified versions of these types (3.9.3) are collectively called POD types"

9(4): "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-define copy 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-define copy operator and no user-defined destructor.

8.5.1(1): "An aggregate is an array or 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)."

 

 

posted @ 2011-07-14 22:17  Lunaa  阅读(458)  评论(0编辑  收藏  举报