我刚刚遇到了一个尴尬的问题,这个问题很容易解决,但我不喜欢这样做。在我的类的构造函数中,我正在初始化数据成员的数据成员。这是一些代码:
class Button {
private:
// The attributes of the button
SDL_Rect box;
// The part of the button sprite sheet that will be shown
SDL_Rect* clip;
public:
// Initialize the variables
explicit Button(const int x, const int y, const int w, const int h)
: box.x(x), box.y(y), box.w(w), box.h(h), clip(&clips[CLIP_MOUSEOUT]) {}
但是,我收到编译器错误:
C:\Users\Alex\C++\LearnSDL\mouseEvents.cpp|56|error: expected `(' before '.' token|
and
C:\Users\Alex\C++\LearnSDL\mouseEvents.cpp|56|error: expected `{' before '.' token|
以这种方式初始化成员是否存在问题,我是否需要切换到构造函数主体中的赋值?
您只能在构造函数中调用成员变量initialization list
。因此,如果SDL_Rect
没有constructor
接受x, y, w, h
,你必须在构造函数的主体中完成它。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)