C ++ pod types

Source: Internet
Author: User

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.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.