Re: [問題] C++ 繼承 object model layout 問題

看板C_and_CPP (C/C++)作者時間8年前 (2017/10/23 22:39), 8年前編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/2 (看更多)
小弟從一本教科書"Inside C++ object model" 裡面提到compiler會mangle class的 member name ===================================書本內容================================== In general, member names are made unique by concatenating the name of the member with that of the class. For example, given the declaration class Bar { public: int ival; ... }; ival becomes something like // a possible member name-mangling ival__3Bar Why does the compiler do that? Consider this derivation: class Foo : public Bar { public: int ival; ... }; Remember that the internal representation of a Foo object is the concatenation of its base and derived class members: // Pseudo C++ Code // internal representation of Foo class Foo { public: int ival__3Bar; int ival__3Foo; ... }; ============================================================================ 這樣了話Hourlyworker internally tranform to class Employee{ public: print_3Employee() {//do Employee method} print_3Hourlyworker() {//do Hourworker method} }; 當 Empoylee *ePtr=(Hourlyworker) &h; compiler則讓ePtr access 到 print_3Hourlyworker 不曉得小弟這樣理解object model對不對? ※ 編輯: gecer (111.255.9.13), 10/23/2017 22:52:02 ※ 編輯: gecer (111.255.9.13), 10/23/2017 23:02:14 ※ 編輯: gecer (111.255.9.13), 10/23/2017 23:02:36 ※ 編輯: gecer (111.255.25.161), 10/24/2017 21:11:42 ※ 編輯: gecer (111.255.25.161), 10/24/2017 21:13:46
文章代碼(AID): #1PxVysM4 (C_and_CPP)
文章代碼(AID): #1PxVysM4 (C_and_CPP)