[C ++ 11] POD Data Type

Source: Internet
Author: User

What is the POD type?

The full name of POD is Plain Old Data. In general, a class or struct can keep its data unchanged after being copied in binary format, so it is a POD type.


Ordinary Definition

1. There are ordinary Constructors

2. There is an ordinary copy constructor

3. There are ordinary mobile Constructors

4. There are ordinary copy assignment operators

5. There are ordinary Moving Value assignment operators

6. There are ordinary destructor

7. cannot contain virtual functions

8. cannot contain virtual base classes


# 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; // there is an extraordinary constructor std: cout <std: is_trivial <B >:: value <std: endl; // an extraordinary copy constructor std: cout <std: is_trivial <C >:: value <std: endl; // there is an extraordinary copy assignment operator std: cout <std: is_trivial <D >:: value <std: endl; // there is an extraordinary copy assignment operator std: cout <std: is_trivial <E >:: value <std: endl; // there is an extraordinary Moving value assignment operator std: cout <std: is_trivial <F >:: value <std: endl; // has an extraordinary destructor std: cout <std: is_trivial <G>: value <std: endl; // has a virtual function std :: cout <std: is_trivial <H>: value <std: endl; // has a virtual base class std: cout <std: is_trivial <I>:: value <std: endl; // ordinary class system ("pause"); return 0 ;}

Running result



Definition of Standard Layout

1. All non-static members have the same access permissions.

2. Only one class can have non-static data members in the inheritance tree.

3. the first non-static member of the subclass cannot be of the base class type.

4. No virtual functions

5. No virtual base class

6. All non-static members comply with the standard layout type.


# 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; // violation Definition 1. Members a and B have different access permissions: std: cout <std: is_standard_layout <B >:: value <std: endl; // violation Definition 2. The inheritance tree has two or more classes with non-static members: std: cout <std: is_standard_layout <C >:: value <std: endl; // violation Definition 3. The first non-static member is the base class type std: cout <std: is_standard_layout <D >:: value <std: endl; // violation Definition 4. There is a virtual function std: cout <std: is_standard_layout <E >:: value <std: endl; // violation of definition 5. There is a virtual base class std: cout <std: is_standard_layout <F >:: value <std: endl; // violation definition 6. Non-static member x does not meet the standard layout type system ("pause"); return 0 ;}

Running result




POD usage

When a data type meets the "ordinary definition" and "Standard Layout", we think it is a POD data. You can use std: is_pod to determine whether a type is of the POD type.

As mentioned at the beginning of the article, a POD type can be copied in binary format. Let's take a look at the following example.


#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;delete p;}system("pause");return 0;}

Running result




We can see that after a binary copy of the POD type is performed, the data is successfully migrated.



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.