samedi 22 septembre 2018

how to initialize a 2D array inside a class while creating and object of it

I have a class with private pointer to pointer (double pointer), which I am using to create a 2D array.

class Arr2D{
    int **arr;
    public:
        Arr2D(int row, int col){
            arr = new int*[r];
            for(int i = 0; i < row; ++i){
                arr[i] = new int[col];
            }
        }
}

I want to initialize this array while creating an object of it as below

int main(){
    Arr2D obj(2,2) = { {1,2}, {3,4} };
} 

how can I initialize the array as show above.

Aucun commentaire:

Enregistrer un commentaire