020: operator[]()

operator[]() を定義します。

引数の範囲外チェックをせずに、heap_[] にアクセスします。もし引数が範囲外の場合は、不定動作となります。

template <typename T>
class myvector
{
...

    /**
     * @brief      Returns a reference to the element at specified location pos.
     *             No bounds checking is performed.
     * @param[in]  pos: Position of the element to return.
     * @return     Reference to the requested element.
     */
    reference operator[](size_type pos)
    {
        return heap_[pos];
    }

    /**
     * @brief      Returns a reference to the element at specified location pos.
     *             No bounds checking is performed.
     * @param[in]  pos: Position of the element to return.
     * @return     Const reference to the requested element.
     */
    const_reference operator[](size_type pos) const
    {
        return heap_[pos];
    }

    ...
};
operator[]() は2種類ありますが、違いは const関数であるか、非const関数であるか、のみです。


全ソースコード: https://github.com/suomesta/myvector/tree/master/020

0 件のコメント:

コメントを投稿