如何在C++中创建一个接受大括号中的参数的类构造函数?
我正在尝试实现一个 Matrix 类,它有一个向量作为其成员。
class Matrix{
public:
Matrix();
Matrix(/*what goes here?*/) : /*here*/
{
/*and here?*/
}
std::vector<std::vector<float>> contents;
}
根据我的作业中的说明,应该可以实现一个构造函数,该构造函数将以与向量向量相同的方式接受数据:
Matrix M({{1, 2, 3}, {4, 5, 6}, {7, 8, 9}});
我的问题是我应该如何在构造函数中指定参数的类型以及如何将它传递给contents成员?
回答
struct Matrix {
std::vector<std::vector<float>> contents;
};
好的设计是尽可能少的设计
——Dieter Rams
- "Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away." — Antoine de Saint-Exupéry