【发布时间】:2020-10-25 14:28:25
【问题描述】:
我在尝试初始化 Context 时遇到以下编译器错误:
引用成员 'context' 绑定到一个临时对象,其生命周期将短于构造对象的生命周期
。编译器指的是什么临时的object? onEventInternal 是 static 所以生命周期不能再短了
class Event {};
class Context {
Context(int width, int height, void(*eventCallback)(Event&)) {}
};
class App {
App(int w, int h): context{w, h, onEventInternal } {} // Error here!
static void onEventInternal(Event& event) {
//event handling
}
private:
const Context& context;
};
【问题讨论】:
标签: c++