检测特定虚拟函数的vtable偏移量(使用Visual C ++) [英] Detect the the vtable offset of a specific virtual function (using Visual C++)

查看:250
本文介绍了检测特定虚拟函数的vtable偏移量(使用Visual C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以检查特定虚拟函数的vtable偏移量?

Can the vtable offset of a specific virtual function be inspected?

为什么?我想要能够检测无意的二进制兼容性中断(请参阅 http:// techbase。 kde.org/Policies/Binary_Compatibility_Issues_With_C%2B%2B 我的意思是二进制兼容性)。

Why? I'd like to be able to detect unintentional binary compatibility breaks (see http://techbase.kde.org/Policies/Binary_Compatibility_Issues_With_C%2B%2B for what I mean by binary compatibility).

我知道/ d1reportSingleClassLayout的无文档和不支持的技术(http://blogs.msdn.com/b/vcblog/archive/2007/05 /17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx),我打算使用这种技术,但我也想使用一些简单的编译时间或运行时检查如果可能的话。

I'm aware of the undocumented and unsupported technique of "/d1reportSingleClassLayout" (http://blogs.msdn.com/b/vcblog/archive/2007/05/17/diagnosing-hidden-odr-violations-in-visual-c-and-fixing-lnk2022.aspx), and I plan to use this technique, but I'd like to also use some simple compile time or run time checks if possible.

推荐答案

受到Jerry的回答的启发,我设法写了这个函数, :

Inspired by Jerry's answer, I managed to write up this function that can do the same thing for any function signature:

#include <iostream>

struct A
{
    virtual void a() {}
    virtual void b() {}
    virtual void c() {}
};

template <class T>
int SeeBits(T func)
{
    union
    {
        T ptr;
        int i;
    };
    ptr = func;

    return i;
}

int main()
{
    int i = SeeBits(&A::a);
    int j = SeeBits(&A::b);
    int k = SeeBits(&A::c);

    std::cout << i << " " << j << " " << k << std::endl;

    return 0;
}

这篇关于检测特定虚拟函数的vtable偏移量(使用Visual C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆