简体   繁体   中英

Declare Array in Class and Initialize it in Constructor

I have the following problem:

I have a class in a header file where I want to declare a two-dimensional array (map).

Then I want to initialize it in the Constructor in the Source-File (cpp) File.

Till now this looks like this:

Headerfile:

class TForm1 : public TForm
{
private:    ...
public:
    __fastcall TForm1(TComponent* Owner);
    int map[][];
};

Sourcefile:

__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 }
};

}

I know this is terribly wrong but I couldnt find any helpful explaination in the internet how it should be done correctly.

Thanks for you help

It is not pretty and flexible. May be you should use Abstract Factory Pattern to do this. And i think you can use std::vector instead of multidimensional arrays. It has many advantages and STL library has reach algorithms to handle with std::vector . For example:

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);

With a help of this pattern you can choose better way to init map and change it in process of development.

In my example I showed 2 factories(hardcode and fileInput) you can think some others later and use.

C++ does not allow to define an array with more than one indeterminate dimension; It is possible to write int[][15] = ... , but it is not legal to write int [][] = ... .

If all of your dimensions are dynamic, I'd suggest to use a vector of vectors. See the following program illustrating this:

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM