根据我对此处指针的理解,currentDancer
和 pushups
应该是同一个引用。然而,如果我设置了 currentDancer
,我将无法使用它。
Dancer *currentDancer;
DancerPushups pushups; // DancerPushups inherits from public Dancer
void setup() {
pushUps.init(FL1, FL2, FR1, FR2, BR1, BR2, BL1, BL2);
currentDancer = &pushups; // try to set pushups as the current dancer
}
void loop() {
pushups.onBeatStart(duration); // works great! :D
currentDancer->onBeatStart(duration); // does nothing D:
}
我在这里的指针处理的哪一部分失败了?如何让 Dancer *currentDancer
指向我已经创建的任意实例?
更新:
这是一个继承问题吗?
void Dancer::onBeatStart(float duration) {
// no-op
}
// DancerPushups : public Dancer
void DancerPushups::onBeatStart(float duration) {
all(the, cool, stuff, that, isnt, happening);
}
因为我正在将 DancerPushups
转换为 Dancer*
类型,我不是在运行我的子类方法吗?
最佳答案
听起来像 onBeatStart
不是一个 virtual
成员函数。当您调用 currentDancer->onBeatStart()
时,您正在调用基类实现,正如您所展示的那样,它什么都不做。
关于c++ - 将指针设置为另一个指针会以某种方式破坏引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23333013/