简体   繁体   中英

How to call parameterized Constructor of Base class in C++?

I have this header (Field)

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)

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

TicTacToeField::TicTacToeField(vector<vector<int>> field) {
    this->field = field;
}

Here is my problem

This is the constructor of my Game 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?

And how do I create a new object? I tried this in my main.cpp but it only works if I extend the default constructor...

TicTacToeGame g("Player1", "Player2");

But this here TicTacToeField(std::vector<std::vector<int>> field)) is wrong [...]

You should be passing the vector of vector of int s to here. 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 . 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 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(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.
  • Use member initializer lists to initialize the members, rather constructing the class and initializing the members as you did.

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
   {}
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM