{"id":432,"date":"2011-03-02T19:48:26","date_gmt":"2011-03-03T02:48:26","guid":{"rendered":"http:\/\/www.cchsu.com\/arthur\/?p=432"},"modified":"2011-03-03T16:07:00","modified_gmt":"2011-03-03T23:07:00","slug":"%e8%ae%80%e6%9b%b8%e7%ad%86%e8%a8%98-inside-c-object-model-2","status":"publish","type":"post","link":"http:\/\/www.cchsu.com\/arthur\/2011\/03\/02\/432\/","title":{"rendered":"\u8b80\u66f8\u7b46\u8a18: Inside the C++ Object Model (2)"},"content":{"rendered":"<p>\u5e95\u4e0b\u662f\u8b49\u660e default constructor \u4e0d\u4e00\u5b9a\u6703\u88ab\u88fd\u9020\u7684\u5be6\u9a57<\/p>\n<span class=\"collapseomatic \" id=\"id69d5624b3f65e\"  tabindex=\"0\" title=\"exp_def_ctor.h\"    >exp_def_ctor.h<\/span><div id=\"target-id69d5624b3f65e\" class=\"collapseomatic_content \">\n<pre lang=\"cpp\" line=\"1\">\r\nclass CFoo\r\n{\r\npublic:\r\n    CFoo();\r\n    \r\n    int get();\r\n    void set(int);\r\n    \r\nprivate:\r\n    int m_x;\r\n};\r\n\r\nclass CFoo2\r\n{\r\npublic:\r\n    int get();\r\n    void set(int);\r\n    \r\nprivate:\r\n    int m_x;\r\n};\r\n\r\nclass CBar\r\n{\r\npublic:\r\n    void set(int);\r\n    int get();\r\n    \r\nprivate:\r\n    CFoo m_foo;\r\n    char* m_szName;    \r\n};\r\n\r\nclass CBar2\r\n{\r\npublic:\r\n    void set(int);\r\n    int get();\r\n    \r\nprivate:\r\n    CFoo2 m_foo;\r\n    char* m_szName;\r\n};\r\n<\/pre>\n<\/div>\n<span class=\"collapseomatic \" id=\"id69d5624b3f7c9\"  tabindex=\"0\" title=\"exp_def_ctor.cpp\"    >exp_def_ctor.cpp<\/span><div id=\"target-id69d5624b3f7c9\" class=\"collapseomatic_content \">\n<pre lang=\"cpp\" line=\"1\">\r\n#include \"exp_def_ctor.h\"\r\n#include <iostream>\r\nusing namespace std;\r\n\r\nCFoo::CFoo() {}\r\nint CFoo::get() { return m_x; }\r\nvoid CFoo::set(int x) { m_x = x; }\r\n\r\nint CFoo2::get() { return m_x; }\r\nvoid CFoo2::set(int x) { m_x = x; }\r\n\r\nint CBar::get() { return m_foo.get(); }\r\nvoid CBar::set(int x) { m_foo.set(x); }\r\n\r\nint CBar2::get() { return m_foo.get(); }\r\nvoid CBar2::set(int x) { m_foo.set(x); }\r\n\r\nvoid main()\r\n{\r\n    CBar bar;\r\n    CBar2 bar2;\r\n    bar.set(3);\r\n    bar2.set(4);\r\n    cout << bar.get() << endl;\r\n    cout << bar2.get() << endl;\r\n}\r\n<\/pre>\n<\/div>\n<span class=\"collapseomatic \" id=\"id69d5624b3f81c\"  tabindex=\"0\" title=\"cppom.mak\"    >cppom.mak<\/span><div id=\"target-id69d5624b3f81c\" class=\"collapseomatic_content \">\n<pre lang=\"dos\" line=\"1\">\r\nCC      = cl\r\nCFLAGS\t= \/EHsc \/Zi \/Fd \/c\r\nLD      = link\r\nLDARGS  = \/INCREMENTAL:NO \/DEBUG\r\nRM\t    = del \/q\r\n\r\n.cpp.obj::\r\n    $(CC) $(CFLAGS) $<\r\n\r\ntest1.exe: exp_def_ctor.obj\r\n\t$(LD) exp_def_ctor.obj $(LDARGS) \/OUT:test1.exe \/PDB:test1.pdb\r\n\r\nclean:\r\n\t$(RM) *.exe *.obj *.pdb *.idb *.ilk *.bak\r\n\r\nall: test1.exe\r\n<\/pre>\n<\/div>\n<p>CFoo2 \u6c92\u6709 constructor\uff0c\u6240\u4ee5 CBar2 \u4e0d\u61c9\u6709 constructor\u3002CFoo \u6709\u4e00\u500b\u6c92\u4f5c\u7528\u7684 constructor\uff0c\u4f46\u5728\u5be6\u9a57\u4e2d\u8b49\u660e VC++ \u4f9d\u7136\u53bb\u547c\u53eb\u5b83<\/p>\n<pre lang=\"dos\">\r\n0:000> ln test1!main\r\nd:\\home\\desktop\\research\\exp_def_ctor.cpp(19)\r\n(00311100)   test1!main   |  (00311170)   test1!std::endl\r\nExact matches:\r\n    test1!main (void)\r\n0:000> u 00311100 0031116f\r\ntest1!main [d:\\home\\desktop\\research\\exp_def_ctor.cpp @ 19]:\r\n00311100 55              push    ebp\r\n00311101 8bec            mov     ebp,esp\r\n00311103 83ec10          sub     esp,10h\r\n00311106 8d4df8          lea     ecx,[ebp-8]\r\n00311109 e882000000      call    test1!CBar::CBar (00311190)\r\n0031110e 6a03            push    3\r\n00311110 8d4df8          lea     ecx,[ebp-8]\r\n00311113 e888ffffff      call    test1!CBar::set (003110a0)\r\n00311118 6a04            push    4\r\n0031111a 8d4df0          lea     ecx,[ebp-10h]\r\n0031111d e8beffffff      call    test1!CBar2::set (003110e0)\r\n00311122 6870113100      push    offset test1!std::endl (00311170)\r\n00311127 8d4df8          lea     ecx,[ebp-8]\r\n0031112a e851ffffff      call    test1!CBar::get (00311080)\r\n0031112f 50              push    eax\r\n00311130 b980083400      mov     ecx,offset test1!std::cout (00340880)\r\n00311135 e896000000      call    test1!std::basic_ostream<char,std::char_traits<char> >::operator<< (003111d0)\r\n0031113a 8bc8            mov     ecx,eax\r\n0031113c e86f000000      call    test1!std::basic_ostream<char,std::char_traits<char> >::operator<< (003111b0)\r\n00311141 6870113100      push    offset test1!std::endl (00311170)\r\n00311146 8d4df0          lea     ecx,[ebp-10h]\r\n00311149 e872ffffff      call    test1!CBar2::get (003110c0)\r\n0031114e 50              push    eax\r\n0031114f b980083400      mov     ecx,offset test1!std::cout (00340880)\r\n00311154 e877000000      call    test1!std::basic_ostream<char,std::char_traits<char> >::operator<< (003111d0)\r\n00311159 8bc8            mov     ecx,eax\r\n0031115b e850000000      call    test1!std::basic_ostream<char,std::char_traits<char> >::operator<< (003111b0)\r\n00311160 33c0            xor     eax,eax\r\n00311162 8be5            mov     esp,ebp\r\n00311164 5d              pop     ebp\r\n00311165 c3              ret\r\n<\/pre>\n<p>\u597d\u50cf\u5f88\u7b28\uff0c\u5982\u679c\u628a\u6700\u4f73\u5316\u958b\u4e0b\u53bb\u6703\u600e\u6a23\u5462\uff1f\u6211\u52a0\u4e86 \/O2 \u4e4b\u5f8c\u5f97\u5230\u4e00\u500b\u7570\u5e38\u8070\u660e\u7684\u7d50\u679c<\/p>\n<pre lang=\"dos\">\r\n0:000> ln test1!main\r\nd:\\home\\desktop\\research\\exp_def_ctor.cpp(19)\r\n(00ba4160)   test1!main   |  (00ba4190)   test1!std::basic_string<char,std::char_traits<char>,std::allocator<char> >::assign\r\nExact matches:\r\n    test1!main (void)\r\n0:000> u 00ba4160 00ba418f\r\ntest1!main [d:\\home\\desktop\\research\\exp_def_ctor.cpp @ 19]:\r\n00ba4160 6a03            push    3\r\n00ba4162 b910f9bc00      mov     ecx,offset test1!std::cout (00bcf910)\r\n00ba4167 e8b4f1ffff      call    test1!std::basic_ostream<char,std::char_traits<char> >::operator<< (00ba3320)\r\n00ba416c 50              push    eax\r\n00ba416d e88effffff      call    test1!std::endl (00ba4100)\r\n00ba4172 83c404          add     esp,4\r\n00ba4175 6a04            push    4\r\n00ba4177 b910f9bc00      mov     ecx,offset test1!std::cout (00bcf910)\r\n00ba417c e89ff1ffff      call    test1!std::basic_ostream<char,std::char_traits<char> >::operator<< (00ba3320)\r\n00ba4181 50              push    eax\r\n00ba4182 e879ffffff      call    test1!std::endl (00ba4100)\r\n00ba4187 83c404          add     esp,4\r\n00ba418a 33c0            xor     eax,eax\r\n00ba418c c3              ret\r\n<\/pre>\n<p>\u5b8c\u5168\u7528\u6578\u503c\u4ee3\u5165\u4e86\uff0c\u4e0d\u904e\u5462\uff0cCBar \u548c CFoo \u4f9d\u7136\u6709 constructor\uff0cCBar2 \u5247\u6c92\u6709<\/p>\n<pre lang=\"dos\">\r\n0:000> ln test1!CBar::CBar\r\n(00ba13f0)   test1!CBar::CBar   |  (00ba1400)   test1!std::_Yarn<char>::_Yarn<char>\r\nExact matches:\r\n    test1!CBar::CBar (void)\r\n0:000> u 00ba13f0 00ba13ff\r\ntest1!CBar::CBar:\r\n00ba13f0 8bc1            mov     eax,ecx\r\n00ba13f2 c3              ret\r\n...\r\n0:000> ln test1!CBar2::CBar2\r\nCouldn't resolve error at 'test1!CBar2::CBar2'\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u95dc\u65bc default constructor \u7684\u5be6\u9a57\u4e00<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,2],"tags":[],"_links":{"self":[{"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts\/432"}],"collection":[{"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/comments?post=432"}],"version-history":[{"count":5,"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts\/432\/revisions"}],"predecessor-version":[{"id":440,"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/posts\/432\/revisions\/440"}],"wp:attachment":[{"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/media?parent=432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/categories?post=432"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cchsu.com\/arthur\/wp-json\/wp\/v2\/tags?post=432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}