0

I have a dynamic array in the class. I have two questions:

  1. I am wondering how to write move constructor and move assignment operator for dynamic array Tile* mytiles?
  2. How can the array be written as a smart pointer uni_ptr? Here is the code:
class Grid
{
private:
    size_t _rows_;
    size_t _cols_;
    Tile _initialTile_;
    Tile* mytiles;

public:
    // Tile is a self defined class
    Grid(size_t rows, size_t cols, const Tile &initialTile): _rows_(rows), 
                                                             _cols_(cols), 
                                                             _initialTile_(initialTile)
    {
        mytiles = new Tile[_cols_ * _rows_];
        std::fill(&(mytiles[0]), &(mytiles[0]) + (_rows_ * 
                 _cols_sizeof(Tile)),_initialTile_);
    }

    Grid(const Grid &other): _rows_(other._rows_), _cols_(other._cols_), 
                             _initialTile_(other._initialTile_)
    {
        mytiles = new Tile[other._rows_ * other._cols_];
        std::copy(other.mytiles, other.mytiles + other._rows_ * other._cols_, mytiles);    
    }

    Grid::operator=(const Grid &other)
    {
        if (this == &other)
        {
            return *this;
        }
        _rows_ = other._rows_;
        _cols_ = other._cols_;
        _initialTile_ = other._initialTile_;
        mytiles = new Tile[other._rows_ * other._cols_];
        std::copy(other.mytiles, other.mytiles + other._rows_ * other._cols_, mytiles);
    }

    Grid(Grid &&other) noexcept: _rows_(std::move(other._rows_)), 
                                 _cols_(std::move(other._cols_)), 
                                 _initialTile_(std::move(other._initialTile_)), 
                                   mytiles(std::move(other.mytiles))
    {
        other.mytiles = nullptr;
    }

    Grid::operator=(Grid &&other) noexcept
    {
         _rows_= std::move(other._rows_);
         _cols_= std::move(other._cols_), 
         _initialTile_= std::move(other._initialTile_);
         delete[] mytiles;
         mytiles = std::move(other.mytiles);
         other.mytiles = nullptr;
         return *this;
    }

};

Tile is an enumertion:

enum Tile {
    See, Sky, Road
};

Tile tile_from_char(const char &c);

char char_from_tile(const Tile &t);

Thank you for your time and help.

4
  • 1
    Change Tile* mytiles; to std::vector<Tile> and just assign it like all the others. Commented Nov 14, 2021 at 11:29
  • Thanks. I tried std::vector<Tile> mytiles before. But in the constructor, the requirement is I must use the std::fill to fill the vector. When I wrote as below, it shows error. ``` Grid(size_t rows, size_t cols, const Tile &initialTile): rows_(rows), _cols_(cols), _initialTile_(initialTile) { mytiles(_rows * cols); // size of the vector std::fill(mytiles.begin(), mytiles.end(), initialTile); } ``` Commented Nov 14, 2021 at 11:34
  • using std::vector<Tile> mytiles you can do std::fill(mytiles.begin(),mytiles.end(),initialTile) Commented Nov 14, 2021 at 11:38
  • The compiler shows an error when I specified the size in constructor Grid(size_t rows, size_t cols, const Tile &initialTile): rows_(rows), _cols_(cols), _initialTile_(initialTile) { mytiles(_rows * cols); // size of the vector std::fill(mytiles.begin(), mytiles.end(), initialTile); } Commented Nov 14, 2021 at 19:50

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.