Notice
Recent Posts
Recent Comments
Link
«   2025/12   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31
Archives
Today
Total
관리 메뉴

nomad-programmer

[Programming/C++] 2차원 배열접근에 대한 연산자 오버로딩의 예 본문

Programming/C++

[Programming/C++] 2차원 배열접근에 대한 연산자 오버로딩의 예

scii 2021. 2. 5. 11:31
#include <iostream>

using namespace std;


class BoundCheckIntArray;

using BoundCheckIntArrayPtr = BoundCheckIntArray*;


class BoundCheckIntArray
{
public:
    explicit BoundCheckIntArray(const int len) : arrlen(len)
    {
        arr = new int[len];
    }

    ~BoundCheckIntArray()
    {
        delete[] arr;
    }

    int& operator[](const int idx)
    {
        if (idx < 0 || idx >= arrlen)
        {
            throw std::out_of_range("Index is out of range");
        }
        return arr[idx];
    }

    int operator[](const int idx) const
    {
        if (idx < 0 || idx >= arrlen)
        {
            throw std::out_of_range("Index is out of range");
        }
        return arr[idx];
    }

    BoundCheckIntArray(const BoundCheckIntArray& ref) = delete;

    BoundCheckIntArray& operator=(const BoundCheckIntArray& ref) = delete;

private:
    int* arr;
    int arrlen;
};


class BoundCheck2DIntArray
{
public:
    BoundCheck2DIntArray(const BoundCheck2DIntArray& ref) = delete;

    BoundCheck2DIntArray& operator=(const BoundCheck2DIntArray& ref) = delete;

    BoundCheck2DIntArray(const int row, const int col)
        : row(row), col(col)
    {
        arr = new BoundCheckIntArrayPtr[row];
        for (int i = 0; i < row; i++)
        {
            arr[i] = new BoundCheckIntArray(col);
        }
    }

    ~BoundCheck2DIntArray()
    {
        // delete each allocated BoundCheckIntArray, then the array of pointers
        for (int i = 0; i < row; ++i)
        {
            delete arr[i];
        }
        delete[] arr;
    }

    // non-const overload
    BoundCheckIntArray& operator[](const int idx)
    {
        if (idx < 0 || idx >= row)
        {
            throw std::out_of_range("Index is out of range");
        }

        return *(arr[idx]);
    }

    // const overload
    const BoundCheckIntArray& operator[](const int idx) const
    {
        if (idx < 0 || idx >= row)
        {
            throw std::out_of_range("Index is out of range");
        }

        return *(arr[idx]);
    }

private:
    int row, col;
    BoundCheckIntArrayPtr* arr;
};


int main()
{
    BoundCheck2DIntArray arr2d(3, 4);

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            arr2d[i][j] = i + j;
        }
    }

    for (int i = 0; i < 3; i++)
    {
        for (int j = 0; j < 4; j++)
        {
            cout << arr2d[i][j] << ' ';
        }
        cout << endl;
    }

    return 0;
}


/* 결과

0 1 2 3
1 2 3 4
2 3 4 5

*/

두 번의 [ ] 연산자 호출을 동반하게끔 구현해야 한다. 즉, 다음과 같이 해석되어야 하며

(arr2d.operator[](i))[j]; 

그리고 arr2d.operator[](i) 연산의 반환 값을 이용해서 두 번째 [ ] 연산을 다음과 같이 해석되어야 한다.

((반환 값).operator[])(j);
Comments