有没有办法用2D数组(int)重载构造函数?

时间:2019-04-26 14:22:36

标签: c++

当我使用默认构造函数时,我希望它调用接受参数的构造函数。但是,这不会正确发生。就调试而言,据我所知,它是在分配值,然后该实例根本就不会维护。我不确定是否需要创建一个辅助方法来传递对象,数组,分配值,然后传递回对象? 我的目标是要有一个默认的构造函数,该构造函数传递一组硬编码的值,然后构造一个接受相同类型的数组作为值传递的构造函数。

我尝试将数组作为构造函数的参数传递,虽然它似乎适用于派生类,但不适用于基类。我最终将重载的构造函数的功能移动到默认构造函数,并且可以正常工作。

这是基类:

// Puzzle.h
class Puzzle
{
public:
    Puzzle();
    Puzzle(int grid[gridLength][gridLength]);
    ~Puzzle();
    void Print_Puzzle(); // Displays puzzle in console

protected:
    int grid[gridLength][gridLength]; // Our board
private:


};

这是定义:

Puzzle::Puzzle()
{
    int grid[gridLength][gridLength] = // Taken from https://www.puzzles.ca/sudoku_puzzles/sudoku_easy_505.html
    {
    { 0, 7, 0, 0, 0, 2, 0, 0, 0 },
    { 0, 9, 0, 3, 7, 0, 0, 0, 0 },
    { 0, 0, 5, 0, 8, 0, 0, 0, 1 },
    { 0, 0, 4, 7, 0, 0, 0, 0, 9 },
    { 0, 0, 0, 0, 9, 6, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 8, 6, 5, 4 },
    { 0, 2, 0, 0, 0, 0, 0, 0, 0 },
    { 0, 0, 0, 0, 0, 1, 0, 4, 3 },
    { 4, 0, 7, 9, 5, 0, 2, 6, 0 }
    };


    for (int x = 0; x < gridLength; x++)
    {
        for (int y = 0; y < gridLength; y++)
            Puzzle::grid[x][y] = grid[x][y];
    }
    // Puzzle::Puzzle(grid) // Doesn't work. Not sure how to properly pass the array values.
}

Puzzle::Puzzle(int grid[gridLength][gridLength])
{
    for (int row = 0; row < gridLength; row++)
    {
        for (int col = 0; col < gridLength; col++)
            this->grid[row][col] = grid[row][col];
    }
}

我希望默认构造函数可以传递grid变量,而接收构造函数可以将这些值分配给实例的member属性。

2 个答案:

答案 0 :(得分:0)

一种更方便且效率也不低的方法是使用可以复制的std::array,这样您就不必逐个元素地复制它:

#include <array>

constexpr int gridLength = 9;
using Grid = std::array<std::array<int, gridLength>, gridLength>;

Grid const grid = {{ // Taken from https://www.puzzles.ca/sudoku_puzzles/sudoku_easy_505.html
{ 0, 7, 0, 0, 0, 2, 0, 0, 0 },
{ 0, 9, 0, 3, 7, 0, 0, 0, 0 },
{ 0, 0, 5, 0, 8, 0, 0, 0, 1 },
{ 0, 0, 4, 7, 0, 0, 0, 0, 9 },
{ 0, 0, 0, 0, 9, 6, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 8, 6, 5, 4 },
{ 0, 2, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 1, 0, 4, 3 },
{ 4, 0, 7, 9, 5, 0, 2, 6, 0 }
}};

class Puzzle {
    Grid grid_;
public:
    Puzzle(Grid const& grid)
        : grid_(grid) // Copy grid into grid_.
    {}
};

int main() {
    Puzzle p(grid);
}

或者:

class Puzzle {
    Grid grid_;

    static Grid make_grid() {
        Grid grid;
        // Your code to fill the grid.
        return grid;
    }

public:
    Puzzle()
        : Puzzle(make_grid())
    {}

    Puzzle(Grid const& grid)
        : grid_(grid) // Copy the grid.
    {}
};

答案 1 :(得分:-1)

我认为您犯了设计错误。 切勿在C ++中使用C数组,而应使用std :: vector或std :: array。 试试这个。

class Sudoku
{
public:

    Sudoku(const std::vector<std::vector<int>> initData = { {/* Write your default values here */} }) 
    {
        data = initData;
    }

private:

    std::vector<std::vector<int>> data;
};

如果要使用类似C的数组,则需要了解传递数组的唯一方法是通过指针,这是一个麻烦的解决方案。

template <uint32_t width, uint32_t height>
class Sudoku
{
public:

    Sudoku(int** initData, int maxX, int maxY)
    {
        for (int i = 0; i < maxX; i++) {
            for (int j = 0; j < maxY; j++) {
                data[i][j] = initData[i][j];
            }
        }
    }

private:

    std::array<width, std::array<height, int>> data;
};