C++ 确定虚拟函数是否重载

C++ 确定虚拟函数是否重载,c++,virtual-functions,C++,Virtual Functions,假设我们有 class Base { virtual void foo() { // some default implementation } }; void bar(Base* b) { } 是否有方法确定b对象的foo函数是否从条函数重载?您不想检测虚拟函数是否已被重写 相反,在函数bar中,如果需要Base实现,可以这样调用它 void bar( Base* p ) { p->Base::foo(); } void bar( B

假设我们有

class Base
{
    virtual void foo()
    {
        // some default implementation
    }
};

void bar(Base* b)
{
}
是否有方法确定
b
对象的
foo
函数是否从
函数重载?

您不想检测虚拟函数是否已被重写

相反,在函数
bar
中,如果需要
Base
实现,可以这样调用它

void bar( Base* p )
{
    p->Base::foo();
}
void bar( Base* p )
{
    p->foo();
}
如果您想要一个虚拟调用,绑定到最派生类中的实现,可以像

void bar( Base* p )
{
    p->Base::foo();
}
void bar( Base* p )
{
    p->foo();
}

一个不可携带、非常脆弱的解决方案就是这个。我不会在真正的程序中使用我自己的解决方案。我只是好奇地想知道这到底能不能做到。我会找到更优雅的方法来解决“真正的问题”。我想,你真正的问题不是四处摆弄,看看函数是否过载。您在其中看到的“1”必须根据基类的布局进行调整。1是函数foo在虚拟表中的位置。需要在bar函数中有一个基类实例

#include "stdafx.h"

class Base { 
    public: 
        int l;
        virtual int foo3(){return 5;}; 
        virtual int foo(){return 0;}; 
};

class Base2 { 
    public: 
        virtual int foo2(){return 2;}; 
};

class Derived : public Base, Base2 { 
    public:
        int j;
        int k;
        virtual int foo(){return 1;}; 
        virtual int foo2(){return 5;}; 
};

void bar(Base* b)
{
    Base d;

    int* p;
    int* p2;

    p = (int*)&d;
    p2 = (int*)b;
    p = (int*)(*p);
    p2 = (int*)(*p2);

    int i = p[1];
    int k = p2[1];
    bool bOverLoaded;
    if(i == k)
    {
        bOverLoaded = false;
    }
    else
    {
        bOverLoaded = true;
    }

}

int _tmain(int argc, _TCHAR* argv[])
{
    Derived* p = new Derived();
    bar(p);
}

你确定你的意思是重载而不是重写吗?顺便说一句,你要查找的单词被重写了。也就是说,当派生类重新实现相同的虚拟方法时。重载表示具有不同参数但名称相同的方法。我需要填充包含函数指针的结构,然后将其传递给第三方库函数。对于默认行为,我需要在该结构中填充NULL。这就是我需要确定的原因it@ArmanHunanyan:抱歉,这个问题似乎与您发布的问题无关。对于原始函数指针,您需要非成员函数或
静态
成员函数。这样的函数不能是
虚拟的
。如果(foo被重写){s.foo_PTR=somting}或者{s.foo_PTR=NULL}然后将s传递给第三方库,我需要在bar中执行以下操作