我有一个基类:
class IScreen {
public:
virtual ~IScreen();
virtual void onEnter() = 0;
};
派生类
class ScreenStandby : public IScreen {
public:
ScreenStandby(ST7565&);
void onEnter();
private:
ST7565& lcd;
};
然后我想要初始化第三个控制器类,它需要从上面开始的基类参数:
class UIController {
public:
UIController(IScreen* left, IScreen* right, Encoder& encoder);
~UIController();
void init();
private:
// Screen for each bay from the left, hardcoded for now
IScreen *left, *right;
};
然后,我使用派生类的新实例初始化此控制器:
UIController UI_CONTROLLER(
new ScreenStandby(LCD_1),
new ScreenStandby(LCD_2),
ENCODER
);
及其定义
UIController::UIController(IScreen* left, IScreen* right, Encoder& encoder)
: left(left), right(right)
{}
然而,当我调用取消引用其中一个派生类指针的UI_Controller.init()时,会抛出异常。
void UIController::init() {
this->right->onEnter(); // Error gets thrown here without ever calling onEnter.
}
我认为我不需要复制构造函数,因为我只传递了引用。我做错了什么?