gpt4 book ai didi

c++ - 如何继承纯虚函数C++的实现

转载 作者:太空宇宙 更新时间:2023-11-04 16:07:53 24 4
gpt4 key购买 nike

<分区>

令我惊讶的是,以下 C++ 代码无法编译。

class InterfaceA {
public:
virtual void Foo() = 0;
};

class InterfaceB : public InterfaceA {
public:
virtual void Bar() = 0;
};

class ImplementationA : public InterfaceA {
public:
void Foo() override {}
};

class ImplementationB : public ImplementationA, public InterfaceB {
public:
void Bar() override {}
};

int main() {
ImplementationB b;
b.Bar();
}

object of abstract class type "ImplementationB" is not allowed: pure virtual function "InterfaceA::Foo" has no overrider. Error C2259 'ImplementationB': cannot instantiate abstract class.

是否可以从实现A中继承纯虚方法的实现,而无需在实现B中重新定义方法Foo:

void Foo() override {
ImplementationA::Foo();
}

如果不是那么为什么?

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com