【发布时间】:2021-08-08 12:47:36
【问题描述】:
我有以下课程:
class ServoPart {
private:
bool move() {
/* move implementation for a singular servo */
}
}
struct RoboPart {
virtual void doJob() =0;
virtual bool move() =0;
}
class Claw : public ServoPart, RoboPart {
private:
void doJob() {/* implementation */}
}
class Arm : RoboPart {
private:
ServoPart major, minor;
void doJob() {/* implementation */}
bool move() {/* implementation for two servos */}
}
现在,Arm 可以工作,因为它同时实现了 doJob 和 move。 Claw 但是不起作用,因为它将是一个抽象类,因为它没有实现移动(即使它有来自基类的 move)。当我将bool move() override; 添加到Claw 时,我从链接器中获得对“vtable for Claw”的未定义引用。
有可能以某种方式做到这一点吗?还是我需要将 ServoPart#move 重命名为 ServoPart#moveStep 并从 Claw 中的函数 move 调用它?
【问题讨论】:
-
为什么
class ServoPart不继承自struct RoboPart?call that from a function move in Claw?您在搜索class Claw ... { bool move() { return ServoPart::move(); } }?private: bool move()你不想要protected:吗? -
@KamilCuk 因为不是每个
ServoPart都是RoboPart。例如,Arm 类有两个ServoParts,它们不是RoboParts。我会将其添加到 OP 中。根据您的编辑,我猜想在Claw::move()中调用ServoPart::move()是没有办法的?
标签: c++ oop overriding abstract-class multiple-inheritance