C++11 pod type (Learn)

Source: Internet
Author: User

What is the pod type? Pod Full name plain old Data. In layman's words, a class or struct can keep its data intact after a binary copy, so it is a pod type. An ordinary definition1. There are trivial constructors2. There are trivial copy constructors3. There are trivial move constructors4. There is a trivial copy assignment operator5. There is a trivial move assignment operator6. There are trivial destructors7. Cannot contain virtual functions8. Cannot contain virtual base class [CPP] View plaincopy on code to view the snippet derivation to my Code slice # include"stdafx.h"#include<iostream>using namespacestd; classa {a () {}}; classb {B (b&){} }; classC {C (c&&){} }; classD {Doperator= (d&){} }; classe {Eoperator= (e&&){} }; classF {~F () {}}; classGVirtual voidFoo () =0; }; classh:g {}; classI {}; int_tmain (intARGC, _tchar*argv[]) {Std::cout<< Std::is_trivial<a>::value << Std::endl;//There is an extraordinary constructorStd::cout << std::is_trivial<b>::value << Std::endl;//There are extraordinary copy constructorsStd::cout << std::is_trivial<c>::value << Std::endl;//There is an extraordinary copy assignment operatorStd::cout << std::is_trivial<d>::value << Std::endl;//There is an extraordinary copy assignment operatorStd::cout << std::is_trivial<e>::value << Std::endl;//There is an extraordinary move assignment operatorStd::cout << std::is_trivial<f>::value << Std::endl;//There is an extraordinary destructorStd::cout << std::is_trivial<g>::value << Std::endl;//there are virtual functionsStd::cout << std::is_trivial//There are virtual base classesStd::cout<< Std::is_trivial<i>::value << Std::endl;//the Ordinary classSystem ("Pause"); return 0; The definition of the run result standard Layout1all non-static members have the same access rights2There can be at most one class in the inheritance tree with non-static data members3the first non-static member of a subclass cannot be a base class type4. No virtual function5. No virtual base class6all non-static members conform to the standard layout type [CPP] view plaincopy on code to view the snippet derivation to my Code slice # include"stdafx.h"#include<iostream>using namespacestd; classA {Private:      intA;  Public:      intb;    }; classB1 {Static intX1;    }; classB2 {intx2;    }; classb:b1, B2 {intx;    }; classC1 {}; classc:c1 {C1 C;    }; classDVirtual voidFoo () =0; }; classE:d {}; classF {A x;}; int_tmain (intARGC, _tchar*argv[]) {Std::cout<< Std::is_standard_layout<a>::value << Std::endl;//violation of definition 1. Members A and B have different access rightsStd::cout << std::is_standard_layout<b>::value << Std::endl;//violation of definition 2. The inheritance tree has two or more classes with non-static membersStd::cout << std::is_standard_layout<c>::value << Std::endl;//violation of definition 3. The first non-static member is a base class typeStd::cout << std::is_standard_layout<d>::value << Std::endl;//violation of definition 4. There are virtual functionsStd::cout << std::is_standard_layout<e>::value << Std::endl;//violation of definition 5. There are virtual base classesStd::cout << std::is_standard_layout<f>::value << Std::endl;//violation of definition 6. Non-static member x does not conform to the standard layout typeSystem ("Pause"); return 0; The use of the run results pod when a data type satisfies "trivial definition" and "standard Layout", we think of it as a pod data. You can use Std::is_pod to determine whether a type is a pod type. As stated at the beginning of the article, a pod type can be binary copy, take a look at the following example. [CPP] View plaincopy on code to see a snippet derived from my Code slice # include"stdafx.h"#include<iostream>#include<Windows.h>using namespacestd; classA { Public:      intx; Doubley;    }; int_tmain (intARGC, _tchar*argv[]) {      if(std::is_pod<a>:: Value) {Std::cout<<"before"<<Std::endl;          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; The results of the operation can be seen, after a binary copy of a pod type, the data is migrated successfully. 

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.