3. 배열 크기를 인자로 받는 생성자(constructor)와 복사 생성자를 추가합니다.

    public:
        dynamic_array(int n)
        {
            this->n = n;
            data = new T[n];
        }
    
        dynamic_array(const dynamic_array<T>& other)
        {
            n = other.n;
            data = new T[n];
    
            for (int i = 0; i < n; i++)
                data[i] = other[i];
        }
    

      4. 멤버 데이터에 직접 접근하기 위한 [] 연산자와 at() 함수를 작성합니다. [] 연산자를 제공함으로써 std::array와 비슷한 방식으로 배열 원소에 접근할 수 있습니다.

    T& operator[](int index)
    {
        return data[index];
    }
    
    const T& operator[](int index) const
    {
        return data[index];
    }
    
    T& at(int index)
    {
        if (index < n)
            return data[index];
        throw "Index out of range";
    }
    
    신간 소식 구독하기
    뉴스레터에 가입하시고 이메일로 신간 소식을 받아 보세요.