关于虚函数的顺序

        一直以来的想法:虚函数在虚函数表中的顺序是按照虚函数在类中定义的先后顺序。实际上并非如此。

今天在看一个宕机dump的时候发现这个事实。

        后来随便测试了下,对于没有重载函数而言,是按照虚函数在类中定义的先后顺序,但是一旦有重载虚函数,形势就变了。

        gcc和vs对于这种情况的实现又不一样。这里只说vs。

        vs对于有重载的虚函数,其在虚函数表中出现的顺序是倒序的。也就是说,重载虚函数越前,其在虚函数表中的位置越靠后。当然,这里还有一个组的概念,也就是说,重载的虚函数总是在一个组中,中间不会掺杂其他的虚函数,即使你重载的虚函数在类中声明的位置不连续。

 

       

 

     此时vtbl的布局如下:

    __vfptr                     const GS::~vftbale

   [0]     GS::realse(void)

   [1]     GS::create(std::string &)

   [2]     GS::create(double)

   [3]     GS::create(int)

   [4]       GS::create(void)

   [5]      GS::save(void)

 

 

参考:

Both Visual C++ and G++ puts functions in order of declaration into the 
VTable. However, if the same name is used for several virtual functions, 
there is a difference: 

- G++ keeps to the order of declaration 
- Visual C++ stores functions into the VTable in reversed order of 
declaration. 

Apparently the reason for VC++ to do this is backwards compatability 
with some pre-windows X86 std. COM had to support that. No better reason 
than that. 

Stay away from virtual functions with the same name if you want cross 
compiler operation. 

 

Neither, it's your code that's incorrect.  Overloading methods isn't 
supported by COM.  The MIDL compiler won't let you define an interface 
with two methods that have same name.  It also won't let you define a 
method that takes reference type as one of it's arguments.  You should 
only use C types in your interface, otherwise you'll probably run into 
other incompatibilites between Visual C++ and MinGW. 

 

http://www.nabble.com/Incorrect-vtable-generation-in-MinGW--td15781052.html

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值