{"id":2762,"date":"2020-10-06T13:04:45","date_gmt":"2020-10-06T05:04:45","guid":{"rendered":"http:\/\/blog.coolcoding.cn\/?p=2762"},"modified":"2020-10-06T13:58:42","modified_gmt":"2020-10-06T05:58:42","slug":"cpod%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/blog.coolcoding.cn\/?p=2762","title":{"rendered":"[C++]POD\u6570\u636e"},"content":{"rendered":"\n<p>POD=Plain Old Data<br>\u5373C\u8bed\u8a00\u7684\u8001\u5e03\u5c40\u6570\u636e<\/p>\n\n\n\n<blockquote class=\"wp-block-quote\"><p>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 called 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. <\/p><\/blockquote>\n\n\n\n<p>C++11\u628aPOD\u5206\u4e3a2\u4e2a\u6982\u5ff5\u96c6\u5408\uff0c\u5e73\u51e1\u5e03\u5c40(trival)\u548c\u6807\u51c6\u5e03\u5c40(standard layout)\uff0c\u540c\u65f6\u6ee1\u8db3\u79f0\u4e3aPOD\u7c7b\u578b<\/p>\n\n\n\n<p>1\u3001\u62e5\u6709\u9ed8\u8ba4\u6784\u9020\u51fd\u6570\u548c\u6790\u6784\u51fd\u6570(trival constructor, trival destructor)<br>2\u3001\u62e5\u6709\u5e73\u51e1\u7684\u590d\u5236\u6784\u9020\u51fd\u6570\u548c\u79fb\u52a8\u6784\u9020\u51fd\u6570\uff08trival copy constructor\uff0ctrival move constructor)<br>3\u3001\u62e5\u6709\u5e73\u51e1\u7684\u590d\u5236\u8d4b\u503c\u8fd0\u7b97\u7b26\u548c\u79fb\u52a8\u8d4b\u503c\u8fd0\u884c\u7b26\uff08trival assignment operator, trival move operator)<br>4\u3001\u4e0d\u80fd\u5305\u542b\u865a\u51fd\u6570\u548c\u865a\u57fa\u7c7b<\/p>\n\n\n\n<p>\u5176\u7b2c1\u30012\u30013\u53ef\u4ee5\u4f7f\u7528=default\u6765\u58f0\u660e\u7f3a\u7701\u7248\u672c<\/p>\n\n\n\n<p>POD\u6570\u636e\u7c7b\u578b\u662fC++\u4e3a\u4e86\u517c\u5bb9C\u7684\u6570\u636e\u3002\u53ef\u4ee5\u4f7f\u7528std::is_trivial&lt;ClassName>::value \u6765\u5224\u65ad\u662f\u5426\u4e3atrivial\u7c7b\u578b<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;iostream>\n\nusing namespace std;\n\n#pragma pack(1)\n\nclass Fruit \n{\n};\n\nclass Stone \n{\n};\n\nclass AppleA :public Fruit \n{\npublic:\n    Fruit childFruit;\n    int size;\n};\n\nclass AppleB :public Fruit\n{\npublic:\n    int weight;\n    Fruit childFruit;\n};\n\nclass AppleC :public Fruit \n{\npublic:\n    Fruit firstFruit;\n    Stone secondStone;\n    Fruit threeFruit;\n    int color;\n};\n\n#pragma pack()\n\n\nint main() \n{\n    AppleA appleA; \n    AppleB appleB;\n    AppleC appleC; \n\n    cout &lt;&lt; \"sizeof(appleA)=\" &lt;&lt; sizeof(appleA) &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleA =\" &lt;&lt; &amp;appleA &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleA.childFruit=\" &lt;&lt; &amp;appleA.childFruit &lt;&lt; endl &lt;&lt; endl;\n\n    cout &lt;&lt; \"sizeof(appleB)=\" &lt;&lt; sizeof(appleB) &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleB =\" &lt;&lt; &amp;appleB &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleB.childFruit=\" &lt;&lt; &amp;appleB.childFruit &lt;&lt; endl &lt;&lt; endl;\n\n    cout &lt;&lt; \"sizeof(appleC)=\" &lt;&lt; sizeof(appleC) &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleC =\" &lt;&lt; &amp;appleC &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleC.firstFruit=\" &lt;&lt; &amp;appleC.firstFruit &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleC.secondStone=\" &lt;&lt; &amp;appleC.secondStone &lt;&lt; endl;\n    cout &lt;&lt; \"&amp;appleC.threeFruit=\" &lt;&lt; &amp;appleC.threeFruit &lt;&lt; endl &lt;&lt; endl;\n\n    return 0;\n}\n\n\u8f93\u51fa\uff1a\n\nsizeof(appleA)=5\n&amp;appleA =012FFA40\n&amp;appleA.childFruit=012FFA40\n\nsizeof(appleB)=5\n&amp;appleB =012FFA30\n&amp;appleB.childFruit=012FFA34\n\nsizeof(appleC)=7\n&amp;appleC =012FFA20\n&amp;appleC.firstFruit=012FFA20\n&amp;appleC.secondStone=012FFA21\n&amp;appleC.threeFruit=012FFA22<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>POD=Plain Old Data\u5373C\u8bed\u8a00\u7684\u8001\u5e03\u5c40\u6570\u636e Arithmetic types (3.9.1),  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[28,6],"tags":[],"_links":{"self":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2762"}],"collection":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=2762"}],"version-history":[{"count":5,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2762\/revisions"}],"predecessor-version":[{"id":2768,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=\/wp\/v2\/posts\/2762\/revisions\/2768"}],"wp:attachment":[{"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2762"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2762"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.coolcoding.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2762"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}