#include <iostream>

#include <cstdlib>

using namespace std;


typedef double* DoubleArrayPtr;


class TwoD

{

public:

    TwoD() {};

    TwoD(int Row, int Column);

    void inputArray();

    double& outputArray(int r, int c);

    friend TwoD operator + (const TwoD & left, const TwoD & right);

    TwoD(const TwoD& Original);

    const TwoD& operator = (const TwoD& right);

    ~TwoD();

private:

    DoubleArrayPtr* Array;

    int Rows;

    int Columns;

};


int main()

{

    int d1, d2;

    cout << "Enter the row and column dimensions of the array:\n";

    cin >> d1 >> d2;

    TwoD m(d1, d2);

    cout << "Enter " << d1 << " rows of "

        << d2 << " doubles each:\n";

    m.inputArray();

    cout << "Echoing the 2 dimensional array:\n";

    for (int i = 0; i < d1; i++)

    {

        for (int j = 0; j < d2; j++)

            cout << m.outputArray(i, j) << " ";

        cout << endl;

    }

    return 0;

}


TwoD::TwoD(int Row, int Column) :Rows(Row), Columns(Column)

{

    Array = new DoubleArrayPtr[Rows];

    for (int i = 0; i < Columns; i++) Array[i] = new double[Columns];


    for (int i = 0; i < Rows; i++)

    {

        for (int j = 0; j < Columns; j++) Array[i][j] = 0;

    }

}


void TwoD::inputArray()

{

    for (int i = 0; i < Rows; i++)

    {

        for (int j = 0; j < Columns; j++) cin >> Array[i][j];

    }

}


double& TwoD::outputArray(int r, int c)

{

    return Array[r][c];

}


TwoD operator+(const TwoD& left, const TwoD& right)

{

    if (left.Rows != right.Rows || left.Columns != right.Columns)

    {

        cout << "Array size is not same.\n";

        exit(1);

    }

    TwoD Return(left.Rows, left.Columns);

    for (int i = 0; i < left.Rows; i++)

    {

        for (int j = 0; j < left.Columns; j++) Return.Array[i][j] = left.Array[i][j] + right.Array[i][j];

    }

    return Return;

}


TwoD::TwoD(const TwoD& Original):Rows(Original.Rows), Columns(Original.Columns)

{

    Array = new DoubleArrayPtr[Rows];

    for (int i = 0; i < Columns; i++) Array[i] = new double[Columns];


    for (int i = 0; i < Rows; i++)

    {

        for (int j = 0; j < Columns; j++) Array[i][j] = Original.Array[i][j];

    }

}


const TwoD& TwoD::operator=(const TwoD& right)

{

    if (Array == right.Array) return right;


    for (int i = 0; i < Rows; i++) delete[] Array[i];

    delete[] Array;


    Rows = right.Rows;

    Columns = right.Columns;


    Array = new DoubleArrayPtr[Rows];

    for (int i = 0; i < Rows; i++) Array[i] = new double[Columns];


    for (int i = 0; i < Rows; i++)

    {

        for (int j = 0; j < Columns; j++) Array[i][j] = right.Array[i][j];

    }


    return right;

}


TwoD::~TwoD()

{

    for (int i = 0; i < Rows; i++) delete[] Array[i];


    delete[] Array;

}


이거 맨 밑에 소멸자 처리 하는 도중에 heap corruption detected error 어쩌고 나오는데

왜 뜨는거임? 

인터넷에 찾아봤을 때는 할당 안된 힙 영역 건드려서 뜨는거라는데 어디서 그런 짓을 한건지 모르겠음..


코딩 왤케 어려워..