{"id":517,"date":"2013-01-26T00:20:58","date_gmt":"2013-01-25T16:20:58","guid":{"rendered":"http:\/\/guoh.ai\/lifelog\/?p=517"},"modified":"2013-01-30T00:07:12","modified_gmt":"2013-01-29T16:07:12","slug":"newbie-cpp-learn-copy-constructor-and-assignment-operator","status":"publish","type":"post","link":"https:\/\/guoh.ai\/lifelog\/2013\/01\/newbie-cpp-learn-copy-constructor-and-assignment-operator\/","title":{"rendered":"[\u83dc\u9e1f\u5b66C++]\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26"},"content":{"rendered":"<p>\u4ec0\u4e48\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4ec0\u4e48\u65f6\u5019\u8981\u7528\uff0c\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u7981\u6b62<br \/>\n<a href=\"http:\/\/blog.csdn.net\/arssqz\/article\/details\/6361986\" target=\"_blank\">http:\/\/blog.csdn.net\/arssqz\/article\/details\/6361986<\/a><\/p>\n<p>\u4e3a\u4ec0\u4e48\u8981\u7981\u6b62\uff0c\u600e\u6837\u7981\u6b62\u62f7\u8d1d\u6784\u9020\u51fd\u6570<br \/>\n<a href=\"http:\/\/jrdodds.blogs.com\/blog\/2004\/04\/disallowing_cop.html\" target=\"_blank\">http:\/\/jrdodds.blogs.com\/blog\/2004\/04\/disallowing_cop.html<\/a><br \/>\n<a href=\"http:\/\/c2.com\/cgi\/wiki?YouArentGonnaNeedIt\" target=\"_blank\">http:\/\/c2.com\/cgi\/wiki?YouArentGonnaNeedIt<\/a><\/p>\n<p>\u7f16\u8bd1\u5668\u5b58\u5728\u54ea\u4e9b\u9677\u9631<br \/>\n<a href=\"http:\/\/www.cnblogs.com\/yfanqiu\/archive\/2012\/05\/08\/2489905.html\" target=\"_blank\">http:\/\/www.cnblogs.com\/yfanqiu\/archive\/2012\/05\/08\/2489905.html<\/a><\/p>\n<p>Google\u548cQt\u7684\u7684\u5b8f\u7684\u533a\u522b<br \/>\n<a href=\"http:\/\/stackoverflow.com\/questions\/1454407\/macros-to-disallow-class-copy-and-assignment-google-vs-qt\" target=\"_blank\">http:\/\/stackoverflow.com\/questions\/1454407\/macros-to-disallow-class-copy-and-assignment-google-vs-qt<\/a><\/p>\n<p>\u5178\u578b\u7684Google\u5b8f\u7684\u5199\u6cd5<br \/>\n<a href=\"http:\/\/src.chromium.org\/svn\/trunk\/src\/third_party\/cld\/base\/macros.h\" target=\"_blank\">http:\/\/src.chromium.org\/svn\/trunk\/src\/third_party\/cld\/base\/macros.h<\/a><\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">\/\/ A macro to disallow the copy constructor and operator= functions\r\n\/\/ This should be used in the private: declarations for a class\r\n\/\/\r\n\/\/ For disallowing only assign or copy, write the code directly, but declare\r\n\/\/ the intend in a comment, for example:\r\n\/\/ void operator=(const TypeName&amp;);  \/\/ DISALLOW_ASSIGN\r\n\/\/ Note, that most uses of DISALLOW_ASSIGN and DISALLOW_COPY are broken\r\n\/\/ semantically, one should either use disallow both or neither. Try to\r\n\/\/ avoid these in new code.\r\n#define DISALLOW_COPY_AND_ASSIGN(TypeName) \\\r\n  TypeName(const TypeName&amp;);               \\\r\n  void operator=(const TypeName&amp;)<\/pre>\n<p>\u4e3b\u8981\u8bb0\u5f55\u81ea\u5df1\u78b0\u5230\u7684\u4e00\u4e2a\u95ee\u9898\uff0c\u7f16\u8bd1\u5668\u663e\u793a\uff1a<\/p>\n<pre class=\"brush: bash; title: ; notranslate\" title=\"\">AbstractClass.h:20:12: cannot declare parameter \u2018&lt;anonymous&gt;\u2019 to be of abstract type \u2018AbstractClass\u2019<\/pre>\n<p>\u5176\u5b9e\u5bf9\u4e8e\u4e00\u4e2a\u6bd4\u8f83\u6709\u7ecf\u9a8c\u7684\u4eba\u6216\u8005\u7406\u8bba\u77e5\u8bc6\u624e\u5b9e\u7684\u4eba\uff0c\u770b\u5230\u8fd9\u884c\u4fe1\u606f\u5c31\u5e94\u8be5\u60f3\u8c61\u5230\u662f\u51fa\u73b0\u4e86\u7c7b\u4f3c\u8fd9\u6837\u7684\u9519\u8bef<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">void im_an_error_method(AbstractClass )\r\n{\r\n     ac.im();\r\n }<\/pre>\n<p>\u53c2\u6570\u6ca1\u6709\u53d8\u91cf\u540d\uff0c\u53c2\u6570\u7c7b\u578b\u662f\u4e00\u4e2a\u62bd\u8c61\u7c7b(\u65e0\u6cd5\u5b9e\u4f8b\u5316)<\/p>\n<p>\u53ef\u60dc\u6211\u65e0\u6cd5\u786e\u5b9a\uff0c\u56e0\u4e3a\u8fd9\u4e2a\u5934\u6587\u4ef6\u4e0d\u662f\u6211\u5199\u7684\uff0c\u6ca1\u6709\u8db3\u591f\u7684\u8bc1\u636e\u53bb\u6307\u51fa\u522b\u4eba\u9519\u8bef\u7684\u5730\u65b9\u3002<br \/>\n\u6240\u4ee5\u6211\u53ea\u80fd\u5c1d\u8bd5\u68c0\u67e5\u81ea\u5df1\u7684\u4ee3\u7801\uff0c\u7ed3\u679c\u53d1\u73b0\u9519\u8bef\u7684\u884c\u6570\u662f\u4ed6\u5934\u6587\u4ef6\u7684<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">DISALLOW_COPY_AND_ASSIGN(AbstractClass);<\/pre>\n<p>\u8fd9\u6837\u4e00\u884c\uff0c\u5f88\u5947\u602a\uff0c\u6309\u9053\u7406\u5e94\u8be5\u8fd9\u4e48\u5199\u662f\u4e0d\u5e94\u8be5\u51fa\u9519\u7684\uff0c\u56e0\u4e3a\u5927\u5bb6\u90fd\u662f\u8fd9\u4e48\u5199\u7684\u3002<br \/>\n\u4e8e\u662f\u6211\u5c1d\u8bd5\u5c06\u8fd9\u884c\u6ce8\u91ca\u6389\uff0c\u6216\u8005\u5c06\u8fd9\u884c\u4ee5\u4ee3\u7801\u7684\u5f62\u5f0f\u624b\u52a8\u5c55\u5f00\u7f16\u8bd1\uff0c\u5373\u8fd9\u6837<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">AbstractClass(const AbstractClass&amp;);\r\nvoid operator=(const AbstractClass&amp;);<\/pre>\n<p>\u53d1\u73b0\u4e5f\u53ef\u4ee5\u7f16\u8bd1\u901a\u8fc7<br \/>\n\u60f3\u60f3\u5230\u8fd9\u91cc\u5e94\u8be5\u53ef\u4ee5\u770b\u660e\u767d\u4e3a\u4ec0\u4e48\u7f16\u8bd1\u5668\u9519\u8bef\u63d0\u793a\u5f53\u4e2d\u6709\u4e2a&#8217;anonymous&#8217;\u9519\u8bef\u4e86\u5427\u3002<\/p>\n<p>\u6700\u7ec8\u539f\u56e0\u5b9a\u4f4d\u5728macro\u6709\u5199\u9519\uff0c\u6240\u4ee5\u5728\u6781\u7aef\u60c5\u51b5\u4e0b\u5c31\u51fa\u7f16\u8bd1\u9519\u8bef\u4e86\u3002<\/p>\n<p>\u6700\u540e\u501f\u7528<a href=\"http:\/\/www.artima.com\/cppsource\/bigtwo.html\" target=\"_blank\">http:\/\/www.artima.com\/cppsource\/bigtwo.html<\/a>\u7684\u4e00\u4e2a\u4f8b\u5b50\u6765\u56de\u987e\u4e0b<\/p>\n<pre class=\"brush: cpp; title: ; notranslate\" title=\"\">class Example {\r\n  SomeResource* p_;\r\npublic:\r\n  Example() : p_(new SomeResource()) {}\r\n  ~Example() {\r\n      delete p_;\r\n   }\r\n};<\/pre>\n<p>\u8fd9\u4f8b\u5b50\u6709\u9519\u8bef\u5417\uff1f\u6709\u51e0\u5904\u9519\u8bef\uff1f<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u4ec0\u4e48\u662f\u62f7\u8d1d\u6784\u9020\u51fd\u6570\uff0c\u4ec0\u4e48\u65f6\u5019\u8981\u7528\uff0c\u4ec0\u4e48\u65f6\u5019\u9700\u8981\u7981\u6b62 http:\/\/blog.csdn.net\/arssqz\/article\/details\/6361986 \u4e3a\u4ec0\u4e48\u8981\u7981\u6b62\uff0c\u600e\u6837\u7981\u6b62\u62f7\u8d1d\u6784\u9020\u51fd\u6570 http:\/\/jrdodds.blogs.com\/blog\/2004\/04\/disallowing_cop.html http:\/\/c2.com\/cgi\/wiki?YouArentGonnaNeedIt \u7f16\u8bd1\u5668\u5b58\u5728\u54ea\u4e9b\u9677\u9631 http:\/\/www.cnblogs.com\/yfanqiu\/archive\/2012\/05\/08\/2489905.html Google\u548cQt\u7684\u7684\u5b8f\u7684\u533a\u522b http:\/\/stackoverflow.com\/questions\/1454407\/macros-to-disallow-class-copy-and-assignment-google-vs-qt \u5178\u578b\u7684Google\u5b8f\u7684\u5199\u6cd5 http:\/\/src.chromium.org\/svn\/trunk\/src\/third_party\/cld\/base\/macros.h \/\/ A macro to disallow the copy constructor and operator= functions \/\/ This should be used in the private: declarations for a class \/\/ \/\/ For disallowing only assign or copy, write the code directly, but declare \/\/ the intend in a comment, for &hellip; <a href=\"https:\/\/guoh.ai\/lifelog\/2013\/01\/newbie-cpp-learn-copy-constructor-and-assignment-operator\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">[\u83dc\u9e1f\u5b66C++]\u62f7\u8d1d\u6784\u9020\u51fd\u6570\u548c\u8d4b\u503c\u8fd0\u7b97\u7b26<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_gfmr_meta_descriptions":[],"_gfmr_multilingual_taxonomy_terms":[],"footnotes":""},"categories":[28],"tags":[],"class_list":["post-517","post","type-post","status-publish","format-standard","hentry","category-cpp"],"_links":{"self":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/517","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/comments?post=517"}],"version-history":[{"count":5,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/517\/revisions"}],"predecessor-version":[{"id":522,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/posts\/517\/revisions\/522"}],"wp:attachment":[{"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/media?parent=517"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/categories?post=517"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/guoh.ai\/lifelog\/wp-json\/wp\/v2\/tags?post=517"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}