[英]How to call parameterized Constructor of Base class in C++?
I have this header (Field)我有这个 header(字段)
class TicTacToeField{
protected:
std::vector<std::vector<int>> field;
public:
TicTacToeField();
TicTacToeField(std::vector<std::vector<int>> field);
};
And this header (Game) which inherits/extends the the class above (Field)而这个 header (Game) 继承/扩展了上面的 class (Field)
class TicTacToeGame : public TicTacToeField {
private:
std::string player1_;
std::string player2_;
int currentPlayer_;
public:
TicTacToeGame(std::string player1, std::string player2);
This is the constructor of the Field class这是字段 class 的构造函数
TicTacToeField::TicTacToeField(vector<vector<int>> field) {
this->field = field;
}
Here is my problem这是我的问题
This is the constructor of my Game class这是我的游戏 class 的构造函数
TicTacToeGame::TicTacToeGame(std::string player1, std::string player2) : TicTacToeField(std::vector<std::vector<int>> field)) {
this->player1_ = player1;
this->player2_ = player2;
this->field = field;
currentPlayer_ = 1;
But this here TicTacToeField(std::vector<std::vector<int>> field))
is wrong and I actually don't know what I should write in the brackets... if I use the default constructor TicTacToeField()
then it's fine but how can I use the parameterized one?但是这里的TicTacToeField(std::vector<std::vector<int>> field))
是错误的,我实际上不知道我应该在括号中写什么......如果我使用默认构造函数TicTacToeField()
那么很好,但我怎样才能使用参数化的呢?
And how do I create a new object?以及如何创建一个新的 object? I tried this in my main.cpp but it only works if I extend the default constructor...我在我的 main.cpp 中尝试了这个,但它只有在我扩展默认构造函数时才有效......
TicTacToeGame g("Player1", "Player2");
But this here
TicTacToeField(std::vector<std::vector<int>> field))
is wrong [...]但是这里的TicTacToeField(std::vector<std::vector<int>> field))
是错误的 [...]
You should be passing the vector of vector of int
s to here.您应该将向量的int
传递到这里。 Meaning, the TicTacToeGame
should have a parameter of std::vector<std::vector<int>>
, which can later be passed to the constructor of the parent class TicTacToeField
.意思是, TicTacToeGame
应该有一个参数std::vector<std::vector<int>>
,稍后可以将其传递给父 class TicTacToeField
的构造函数。 Example例子
TicTacToeGame(std::string player1, std::string player2, std::vector<std::vector<int>> field = {})
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ provided as default arguments
: TicTacToeField{ std::move(field) } // resources (preferably) can be moved, rather copy constructing them.
, player1_{ std::move(player1) } // Otherwise, pass by const reference
, player2_{ std::move(player2) }
, currentPlayer_{ 0 }
{}
Since you provide the parent constructor argument as default argument , now you have the following two options to construct the TicTacToeGame
.由于您提供了父构造函数参数作为默认参数,现在您有以下两个选项来构造TicTacToeGame
。
TicTacToeGame game{ "player1", "player2" };
// or pass std::vector<std::vector<int>>
TicTacToeGame game2{ "player1", "player2", {{1, 2, 3},{4, 5}} };
However, if you do not have mField
available at the moment of constructing TicTacToeGame
, you could either invoke the default constructor of TicTacToeField
(ie parent class) or pass a default constructed std::vector<std::vector<int>>
to chose the parameterized constructor of the TicTacToeField
.但是,如果在构造TicTacToeGame
时没有可用的mField
,则可以调用TicTacToeField
的默认构造函数(即父类)或传递默认构造的std::vector<std::vector<int>>
来选择TicTacToeField
的参数化构造函数。
TicTacToeGame(std::string player1, std::string player2)
: TicTacToeField{ std::vector<std::vector<int>>{} }
// or simply default constructor : TicTacToeField{}
, player1_{ std::move(player1) }
, player2_{ std::move(player2) }
, currentPlayer_{ 0 }
{}
A couple of suggestions:几个建议:
class TicTacToeField
{
std::vector<std::vector<int>> mField; // class member `mField`
public:
TicTacToeField(std::vector<std::vector<int>> field) // parameter `field`
: mField{ std::move(field) } // member initializer lists
{}
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.