Why does this code not need a move-ctor?

My textbook is giving an example of a generic class to store generic objects for a game board without using templates. Here is the abbreviated pseudocode:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 class Gamepiece
 {
   public:
     virtual unique_ptr<Gamepiece> clone() const = 0
 };
//-------------------------------
 class Chesspiece : public Gamepiece 
 {
    public:
      virtual unique_ptr<Gamepiece> clone() const override
       {return make_unique<Chesspiece> (*this);}
 };
//---------------------------------------
 class Gameboard
 {
    public:
      explicit Gameboard (size_t width = KDEFAULTWIDTH, size_t height = KDEFAULTHEIGHT);
      Gameboard(const& src); 
      Gameboard& operator= (Gameboard& rhs); //same

      Gameboard(Gameboard&& src) = default; // why?
      Gameboard& operator=(Gameboard&& rhs) = default //why?

      //code omitted for brevity

    private:
       size_t mWidth, mHeight;
       vector<vector<unique_ptr<GamePiece>>> mCells;
};    

//---------------
//code omitted for brevity:

//copy-ctor definition
Gameboard::Gameboard (const Gameboard& src)
   : Gameboard (src.mWidth, src.mHeight)
{
 
 for (size_t i = 0; i < mWidth, i++) {
  for (size_t j = 0; j < mHeight; j++) {
    if (src.mCells[i][j])
      mCells[i][j] = src.mCells[i][j]->clone()
   }
  }
}


My question is, why does the author implement a custom copy ctor and copy assignment but defaulted move semantics? Also why does he implement a strange "clone" method he uses in the copy-ctor?
Last edited on
the move-constructor does what the author wants (move the vector)
the copy-constructor would try a shallow copy, then fail because unique_ptr is not copyable, so you need to provide a copy-constructor

now, you store pointers to `GamePieces' and you want to copy
if the GamePiece is a knigth, then you need to copy a knight
https://katyscode.wordpress.com/2013/08/22/c-polymorphic-cloning-and-the-crtp-curiously-recurring-template-pattern/
Topic archived. No new replies allowed.