C++ 在类中声明数组并在构造函数中初始化它
我有以下问题: 我在头文件中有一个类,我想在其中声明一个二维数组(map) 然后我想在源文件(cpp)的构造函数中初始化它 到目前为止,这看起来像这样: 头文件:C++ 在类中声明数组并在构造函数中初始化它,c++,arrays,initialization,declaration,C++,Arrays,Initialization,Declaration,我有以下问题: 我在头文件中有一个类,我想在其中声明一个二维数组(map) 然后我想在源文件(cpp)的构造函数中初始化它 到目前为止,这看起来像这样: 头文件: class TForm1 : public TForm { private: ... public: __fastcall TForm1(TComponent* Owner); int map[][]; }; 源文件: __fastcall TForm1::TForm1(TComponent* Owner) :
class TForm1 : public TForm
{
private: ...
public:
__fastcall TForm1(TComponent* Owner);
int map[][];
};
源文件:
__fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) {
map[][] = { {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
{ 1, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 1, 1, 0, 1, 1, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 2, 0, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 },
{ 1, 7, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 7, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 3, 1, 1, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 3, 3, 3, 3, 3, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 3, 12, 3, 13, 3, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 3, 3, 14, 3, 3, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 0, 1, 0, 1 },
{ 1, 0, 0, 2, 0, 0, 0, 5, 0, 0, 0, 2, 0, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 3, 1, 0, 1, 3, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1 },
{ 1, 0, 0, 0, 0, 0, 0, 0, 0, 11, 0, 0, 0, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 1 },
{ 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1 },
{ 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1 },
{ 1, 2, 0, 0, 0, 0, 0, 7, 0, 0, 0, 0, 0, 2, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};
}
我知道这是非常错误的,但我在互联网上找不到任何有用的解释,说明如何正确地做
谢谢你的帮助它既不美观又不灵活。可能您应该使用抽象工厂模式来实现这一点。我认为可以使用
std::vector
代替多维数组。它有许多优点,STL库有reach算法来处理std::vector
。例如:
class IMapInitializer
{
public:
virtual void InitMap(std::vector<std::vector<int>>& map) = 0;
virtual ~IMapInitializer(){}
};
class HardcordeMapInitializer : public IMapInitializer
{
public:
virtual void InitMap(std::vector<std::vector<int>>& map) override
{
map = {{1,2,3},{1,2,3},{1,2,3}};
}
};
class FileMapInitializer : public IMapInitializer
{
public:
virtual void InitMap(std::vector<std::vector<int>>& map) override
{
//Read map from file
}
};
//...
class TForm1 : public TForm
{
private: ...
public:
__fastcall TForm1(TComponent* Owner, IMapInitializer& mapInitializer );
std::vector<std::vector<int>> map;
};
//...
__fastcall TForm1::TForm1(TComponent* Owner, IMapInitializer& mapInitializer ) : TForm(Owner) {
mapInitializer.InitMap(map);
类imapinInitializer
{
公众:
虚拟void InitMap(std::vector&map)=0;
虚拟~imapinInitializer(){}
};
类硬记录IMapInitializer:公共IMapInitializer
{
公众:
虚拟void InitMap(std::vector&map)覆盖
{
map={1,2,3},{1,2,3},{1,2,3};
}
};
类FileMapInitializer:公共IMAPIInitializer
{
公众:
虚拟void InitMap(std::vector&map)覆盖
{
//从文件中读取地图
}
};
//...
类TForm1:公共TForm
{
二等兵:。。。
公众:
__fastcall TForm1(t组件*所有者、IMAPIInitializer和mapInitializer);
std::矢量地图;
};
//...
__fastcall TForm1::TForm1(TComponent*Owner、IMAPIInitializer和mapInitializer):TForm(Owner){
mapInitializer.InitMap(映射);
有了这个模式的帮助,您可以选择更好的初始化映射的方法,并在开发过程中对其进行更改
在我的示例中,我展示了两个工厂(硬代码和文件输入),您可以在以后考虑使用其他工厂。C++不允许定义具有多个不确定维度的数组;可以编写
int[][15]=…
,但编写int[][]=…
是不合法的
如果所有维度都是动态的,我建议使用向量向量。请参阅下面的程序来说明这一点:
class ClassWith2DArray {
public:
ClassWith2DArray();
vector<vector<int>> map;
};
ClassWith2DArray::ClassWith2DArray() : map ({{2,3},{3,4}}) {}
int main()
{
ClassWith2DArray c;
for (auto row : c.map) {
for (auto column : row) {
cout << column << " ";
}
cout << endl;
}
return 0;
}
ClassWith2DArray类{
公众:
ClassWith2DArray();
矢量地图;
};
ClassWith2DArray::ClassWith2DArray():映射({2,3},{3,4}){
int main()
{
用2dc数组分类;
用于(自动行:c.map){
用于(自动列:行){
从C++11开始,您可以按值返回std::vector
,而不会受到惩罚…但是,直接传递std::vector
似乎比工厂更好。感谢您的帮助-这对我很有帮助感谢您的努力-这很有帮助:)