简体   繁体   English

如何在C++中调用Base class的参数化构造函数?

[英]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:几个建议:

  • Provide different names to the constructor arguments and the members of the class.为构造函数 arguments 和 class 的成员提供不同的名称。
  • Use member initializer lists to initialize the members, rather constructing the class and initializing the members as you did.使用成员初始化器列表来初始化成员,而不是像您一样构造 class 并初始化成员。

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.

相关问题 如何在 C++ 的类的默认构造函数中调用成员 object 变量的参数化构造函数? - How to call parameterized constructor of member object variable in a class' default constructor in C++? 在C ++中派生类构造函数之后调用基类构造函数 - Call base class constructor after the derived class constructor in C++ c++ 中派生的 class 构造函数中的动态基 class 构造函数调用 - dynamic base class constructor call in derived class constructor in c++ C++派生类构造函数调用基类构造函数错误 - C++ derived class constructor call base class constructor errors c++ - 如何从模板基类的构造函数调用模板超类的构造函数? - How to call a template super class's constructor from a template base class's constructor in c++? 模板化类中的C ++参数化构造函数 - C++ parameterized constructor In a templated class 如何在C ++中从单个对象调用参数化和非参数化构造函数 - how to call parameterized and non parameterized constructor from single object in c++ 用相同的参数调用基类的C ++构造函数 - Call C++ constructor of base class with same arguments C++ 避免调用抽象基类的构造函数 - C++ avoid call to abstract base class's constructor 稍后在 C++ 中调用基类构造函数(不在初始化列表中) - Call a base class constructor later (not in the initializer list) in C++
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM