【发布时间】:2020-09-07 19:54:34
【问题描述】:
我正在学习 C++,我对多重继承和接口类感到困惑。
我想要一个从其他几个继承的类。另外,我想通过接口使用该派生类。所以我想,派生类应该扩展基类,派生接口应该扩展基接口。我会用其他语言做到这一点,但我认为 C++ 不能那样工作。
这是我认为应该可以工作的代码:
#include <iostream>
using std::cout;
using std::endl;
class
Base1Itf
{
public:
virtual void blue() = 0;
};
class
Base1Abs
:
public Base1Itf
{
public:
void blue()
{
cout << "blue" << endl;
}
};
class
DerivedItf
:
public Base1Itf
{
public:
virtual void red() = 0;
};
class
Derived
:
public Base1Abs,
public DerivedItf
{
public:
void red()
{
cout << "red" << endl;
}
};
int main()
{
DerivedItf* d = new Derived();
d->red();
d->blue();
delete d;
return 0;
}
这是我得到的编译器错误:
src/test.cpp: In function ‘int main()’:
src/test.cpp:49:30: error: invalid new-expression of abstract class type ‘Derived’
DerivedItf* d = new Derived();
^
src/test.cpp:35:2: note: because the following virtual functions are pure within ‘Derived’:
Derived
^~~~~~~
src/test.cpp:10:16: note: virtual void Base1Itf::blue()
virtual void blue() = 0;
^~~~
在示例中只实现了一个基类,但还会有更多。
我做错了什么?谢谢。
编辑
如果我删除 Base1Itf 的 Base1Abs 继承来避免 Diamond 问题,编译器会显示相同的错误。
【问题讨论】:
-
你不能用pure virtual function创建一个类的对象
-
@d4rk4ng31 是的,我通过阅读编译器错误知道这一点。但如果可能的话,我不知道该怎么做。
-
@d4rk4ng31 是的,我知道,但是正如标题所说,我想通过接口使用派生类。
-
你必须在
Derived中实现blue()。 LIVE
标签: c++ inheritance interface multiple-inheritance