×

Loading...

基本正确, but should not be compiler dependent. 这个问题我在五年前花了些时间研究, 后来在面试时还考了几个candidates. 问了三次, 两个印度人都答对, 一个俄国人答错.

本文发表在 rolia.net 枫下论坛Sorry I have to write the rest in English. The ANCI C++ working paper indicates that "inline" is not required, but should be respected.

If a compiler is implemented correctly, it should handle inline virtual as follows: the function can be inlined if the compiler knows the actual object. However if the method invocation is via a pointer or a reference, then the inline part should be ignored by the compiler since which method to be invoked will be determined at runtime. Some compilers may decide to inline a method invoked via a pointer to an object whose dynamic type is known at compile time, which is fine.

Also some compilers decide where to put the vtbl based on that translation unit where you defined the first (or last or whatever) non-pure,non-inline virtual function, so if you happen to use such a compiler and have declared *all* your virtual function inline, this could be a problem.

Compilers are occasionally implemented incorrectly. I remember on the average one out of six compilers handle smart pointer incorrectly by deleting an extra reference counting due to promotion. As a result three years later (or even longer, or maybe never) the application crashes due to the client code trys to access a non exist object. These kinds of bugs are extreamly to find.更多精彩文章及讨论,请光临枫下论坛 rolia.net
Report