通过指向派生类的函数调用基本虚方法

问题描述 投票:6回答:2

我需要通过指针从派生类调用基本方法A :: foo()。

#include <iostream>
struct A{
    virtual void foo() { std::cout << "A::foo()" << std::endl; }
};

struct B:A{
    virtual void foo() { std::cout << "B::foo()" << std::endl; }
    void callBase(void (A::*f)()){
        (this->*f)();
    }
};

int main(){
    B* p=new B();
    p->callBase(&A::foo);
}

此代码输出“B :: foo”。是否可以通过指向方法的方法调用A :: foo()?

c++ inheritance polymorphism function-pointers
2个回答
1
投票

好吧,你可以用一些技巧覆盖this的值来做类似的事情。你可能永远不应该尝试这样做,vtable指针不是要手工修改。

要做你所描述的,我们需要指向A的vtable。我们的对象p只有指向B的vtable的指针,所以我们需要在A的构造函数中的一个字段中存储第二个指针。

这是代码:

#include <iostream>
struct A{
    virtual void foo() { std::cout << "A::foo()" << std::endl; }
    int *a_vtable_ptr;
    // First, save value of A's vtable pointer in a separate variable.
    A() { a_vtable_ptr = *(int**)this; }
};

struct B:A{
    virtual void foo() { std::cout << "B::foo()" << std::endl; }
    void callBase(void (A::*f)()){
        int *my_vtable_ptr = *(int**)this;
        // Then modify vtable pointer of given object to one that corresponds to class A.
        *(int**)this = a_vtable_ptr;
        (this->*f)(); // Call the method as usual.
        // Restore the original vtable pointer.
        *(int**)this = my_vtable_ptr;
    }
};

// Function main() is not modified.
int main(){
    B* p=new B();
    void (A::*f)() = &A::foo;
    p->callBase(f);
}

输出:

A::foo()

Process finished with exit code 0

1
投票

虚方法旨在实现多态,而指向虚方法的指针支持其多态行为。但是您可以通过显式调用p->A::foo()来调用基本方法。

因此,如果要通过指针调用基本方法,则应将其设置为非虚拟(如注释中提到的@PasserBy)。

代码示例:

struct A {
    virtual void foo() { std::cout << "A::foo()" << std::endl; }
    void bar() { std::cout << "A::bar()" << std::endl; }
    void callBase(void (A::*f)()) { (this->*f)(); }
};

struct B : A {
    virtual void foo() { std::cout << "B::foo()" << std::endl; }
    void bar() { std::cout << "B::bar()" << std::endl; }
};

int main()
{
    A* p = new B();
    p->foo();
    p->bar();
    p->callBase(&A::foo);
    p->callBase(&A::bar);
    p->A::foo();
    p->A::bar();
}

输出:

B::foo()
A::bar()
B::foo()
A::bar()
A::foo()
A::bar()
© www.soinside.com 2019 - 2024. All rights reserved.