[問題] c++ virtual method的故事
最近看到LLVM裡面的某一點coding standard讓我一直想不透
If a class is defined in a header file and has a vtable (either it has
virtual methods or it derives from classes with virtual methods), it must
always have at least one out-of-line virtual method in the class. Without
this, the compiler will copy the vtable and RTTI into every .o file that
#includes the header, bloating .o file sizes and increasing link times.
大意就是,在一個定義在header file裡的class有virtual method or 它繼承的base
class有virtual method的話,
就必須至少定義一個virtual method
沒有的話compiler會copy vtable 和RTTI到每個有include這個header的.o檔
我的問題是,為什麼有一個virtual method compiler就不copy了?
所以vtable會放哪?
想知道整個故事是到底怎麼回事
有去看過more effective c++ item24 但是還是不太懂
只知道compiler copy是要解決vtable放哪的問題
請問有人知道嗎
感謝
====
知道了
http://www.angelfire.com/weird2/darkmoon/MEC/MI24_FR.HTM
裡面提到
A more common design is to employ a heuristic to determine which object file
should contain the vtbl for a class. Usually this heuristic is as follows: a
class's vtbl is generated in the object file containing the definition (i.e.,
the body) of the "first non-inline non-pure" virtual function in that class.
If all virtual functions in a class are declared inline, the heuristic fails,
and most heuristic-based implementations then generate a copy of the class's
vtbl in every object file that uses it.
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 61.230.112.113
→
04/12 00:12, , 1F
04/12 00:12, 1F
推
04/12 00:25, , 2F
04/12 00:25, 2F
→
04/12 00:27, , 3F
04/12 00:27, 3F
→
04/12 00:28, , 4F
04/12 00:28, 4F
→
04/12 00:29, , 5F
04/12 00:29, 5F
→
04/12 00:34, , 6F
04/12 00:34, 6F
※ 編輯: a9228097 來自: 61.230.112.113 (04/12 00:38)
推
04/12 00:47, , 7F
04/12 00:47, 7F
→
04/12 00:48, , 8F
04/12 00:48, 8F
→
04/12 23:37, , 9F
04/12 23:37, 9F
→
04/12 23:39, , 10F
04/12 23:39, 10F
→
04/13 00:17, , 11F
04/13 00:17, 11F
→
04/13 00:18, , 12F
04/13 00:18, 12F
C_and_CPP 近期熱門文章
PTT數位生活區 即時熱門文章