We first saw the pod (plain old data) type in imperfect C ++. I think this is a very important book that has brought me into the C ++ world.
The book explains pod as follows:
1. All scalar types (Basic Types and pointer types), POD structure types, POD Union types, arrays of these types, const/volatile modified versions
These are all pod types.
2. Pod structure/Joint Type: A combination (including class), its non-static members are not pointer to class member,
Pointer to class member function, non-pod structure, non-pod union, and arrays, references, const/
Version modified by volatile;
In addition, this aggregate cannot have user-defined constructor, destructor, or copy constructor.
3. the pod type can include static members, member typedef, nested struct/class definitions, and member functions/methods.
(C ++ standard:
Copy each byte of the object to a byte array, and then copy it to the storage area occupied by the original object. At this time, the object should have its original value.
Pod type features:
All pod types can be union members. Otherwise, all non-pod types cannot be union members.
Pod features utilization:
We can use the pod Type feature to determine whether a type is a pod type:
Template <class T> struct must_be_pod
{
Union
{
T noname;
};
};
This template means that as long as type T is not of the pod type, the compiler will report an error because T is a member of the Union.
The test code in vs2008 is as follows:
Class
{
Public:
A (){}
Void F () {cout <"A: F" <Endl ;}
Protected:
PRIVATE:
Int I;
Int J;
};
Template <class T> struct must_be_pod
{
Union
{
T noname;
};
};
Must_be_pod <A> A; the compiler reports the following error: 1> member 'must _ be_pod <t >:: noname' of Union 'must _ be_pod <t> :: <unnamed-tag> 'has User-Defined constructor or non-trivial default constructor
In fact, the pod is essentially a data type compatible with C.