简体   繁体   English

C++ 将指针转换为基 class 用于虚方法调用的指针

[英]C++ cast pointer to base class pointer for virtual method call

I have the following code:我有以下代码:

struct A 
{
    virtual void foo() {std::cout << "A\n";}
};

struct B : public A 
{
    virtual void foo() {std::cout << "B\n";}
};

void bar(A * a) 
{
    a->foo(); 
}

Without changing this code, is it possible to cast bp pointer to B, so calling bar would print "A"?在不更改此代码的情况下,是否可以将bp指针转换为 B,以便调用bar会打印“A”?

int main()
{
    B * bp = new B();
    bar(/* do somethig*/ bp);
    return 0;
}

Tried every cast I remebered:尝试了我记得的每一个演员:

int main()
{
    B * bp = new B();
    
    bar((A*)bp);
    bar(static_cast<A*>(bp));
    bar(reinterpret_cast<A*>(bp));
    bar(dynamic_cast<A*>(bp));
    return 0;
}

You could make a shim wrapper around B , and have the shim's virtual function dispatched to BWrap::foo() call directly to A::foo();您可以围绕B制作填充程序包装器,并将填充程序的虚拟 function 分派给BWrap::foo()直接调用A::foo(); . .

There's not really any point in the example to carrying along the B& member variable reference, but for more interesting examples there may be a use case.在示例中携带B&成员变量引用实际上没有任何意义,但对于更有趣的示例,可能有一个用例。

struct BWrap : public A
{
    B& b;
    BWrap(B& bb) : b{bb} {}
    virtual void foo() { b.A::foo(); }
};

int main()
{
    B* bp = new B();
    BWrap bw{*bp};
    bar(&bw);
}

If you insist on the A object being a base class subobject of a B object and on not modifying the first code snippet at all, then the only solution is to add an even more derived class that can override the virtual call as explained in the answer by @Eljay (which I completely forgot to think about when first writing this answer).如果您坚持A object 是B object 的基础 class 子对象,并且根本不修改第一个代码片段,那么唯一的解决方案是添加一个更派生的 class ,它可以覆盖虚拟调用,如答案中所述作者 @Eljay(我在第一次写这个答案时完全忘记了考虑)。

Other options are to create a complete A object, not a B object, or to modify bar to do a call without virtual dispatch by using a qualified name:其他选项是创建一个完整A object,而不是B object,或者修改bar以使用限定名称在没有虚拟调度的情况下进行呼叫:

a->A::foo();

All of the casts you are showing have the same effect as the implicit conversion, except for reinterpret_cast which will cause undefined behavior when used this way.您显示的所有强制转换都与隐式转换具有相同的效果,但reinterpret_cast除外,它在以这种方式使用时会导致未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM