【发布时间】:2017-12-31 01:24:44
【问题描述】:
我有一个问题。我想从“Game”类中调用“gameWindow”的构造函数。问题是,如果我从构造函数调用它,它将初始化为局部变量(例如 A),如果我将它定义为私有成员 - 我不能使用构造函数的参数。如何让 gamewindowObj 成为构造函数的成员?
//示例А
class Game{
public:
Game(int inWidth, int inHeight, char const * Intitle);
};
Game::Game(int inWidth, int inHeight, char const * Intitle){
gameWindow gamewindowObj=gameWindow(inWidth, inHeight, Intitle);
}
//例子В
class Game{
public:
Game(int inWidth, int inHeight, char const * Intitle);
private:
gameWindow gamewindowObj=gameWindow(inWidth, inHeight, Intitle);
};
Game::Game(int inWidth, int inHeight, char const * Intitle){}
【问题讨论】:
-
使用构造函数初始化列表:
Game(int inWidth,....) : gamewindowObj(inWidht,...) {}
标签: c++ oop constructor initialization