演算子オーバーロード 解答ページ | Programming Place Plus C++編【言語解説】 第19章

トップページC++編

C++編で扱っている C++ は 2003年に登場した C++03 という、とても古いバージョンのものです。C++ はその後、C++11 -> C++14 -> C++17 -> C++20 -> C++23 と更新されています。
なかでも C++11 での更新は非常に大きなものであり、これから C++ の学習を始めるのなら、C++11 よりも古いバージョンを対象にするべきではありません。特に事情がないなら、新しい C++ を学んでください。 当サイトでは、C++14 をベースにした新C++編を作成中です。

問題①

問題① この章の解説で登場した DataStoreクラスの全体像を完成させてください。このクラスは、int型の値を1つ管理するだけの(それほど意味のない)クラスです(あなたが便利であると思う形に調整を加えて構いません)。


実装可能な演算子のほとんどを入れ込んでみると、次のような感じになります。

// DataStore.h

class DataStore {
public:
    DataStore();
    explicit DataStore(int value);
    DataStore(const DataStore& rhs);

    inline int ToInt() const
    {
        return mValue;
    }


    inline bool operator==(const DataStore& rhs) const
    {
        return mValue == rhs.mValue;
    }
    inline bool operator!=(const DataStore& rhs) const
    {
        return !(*this == rhs);
    }
    inline bool operator<(const DataStore& rhs) const
    {
        return mValue < rhs.mValue;
    }
    inline bool operator>(const DataStore& rhs) const
    {
        return mValue > rhs.mValue;
    }
    inline bool operator<=(const DataStore& rhs) const
    {
        return !(*this > rhs);
    }
    inline bool operator>=(const DataStore& rhs) const
    {
        return !(*this < rhs);
    }

    const DataStore operator+(const DataStore& rhs) const;
    const DataStore operator-(const DataStore& rhs) const;
    const DataStore operator*(const DataStore& rhs) const;
    const DataStore operator/(const DataStore& rhs) const;
    const DataStore operator%(const DataStore& rhs) const;

    const DataStore operator&(const DataStore& rhs) const;
    const DataStore operator|(const DataStore& rhs) const;
    const DataStore operator^(const DataStore& rhs) const;
    const DataStore operator~() const;
    const DataStore operator<<(const DataStore& rhs) const;
    const DataStore operator>>(const DataStore& rhs) const;

    inline DataStore& operator+=(const DataStore& rhs)
    {
        mValue += rhs.mValue;
        return *this;
    }
    inline DataStore& operator-=(const DataStore& rhs)
    {
        mValue -= rhs.mValue;
        return *this;
    }
    inline DataStore& operator*=(const DataStore& rhs)
    {
        mValue *= rhs.mValue;
        return *this;
    }
    inline DataStore& operator/=(const DataStore& rhs)
    {
        mValue /= rhs.mValue;
        return *this;
    }
    inline DataStore& operator%=(const DataStore& rhs)
    {
        mValue %= rhs.mValue;
        return *this;
    }
    inline DataStore& operator&=(const DataStore& rhs)
    {
        mValue &= rhs.mValue;
        return *this;
    }
    inline DataStore& operator|=(const DataStore& rhs)
    {
        mValue |= rhs.mValue;
        return *this;
    }
    inline DataStore& operator^=(const DataStore& rhs)
    {
        mValue ^= rhs.mValue;
        return *this;
    }
    inline DataStore& operator<<=(const DataStore& rhs)
    {
        mValue <<= rhs.mValue;
        return *this;
    }
    inline DataStore& operator>>=(const DataStore& rhs)
    {
        mValue >>= rhs.mValue;
        return *this;
    }


    inline const DataStore operator+() const
    {
        return *this;
    }
    const DataStore operator-() const;

    inline DataStore& operator++()
    {
        ++mValue;
        return *this;
    }
    inline DataStore& operator--()
    {
        --mValue;
        return *this;
    }
    const DataStore operator++(int);
    const DataStore operator--(int);

    inline const int* operator&() const
    {
        return &mValue;
    }

    inline bool operator!() const
    {
        return mValue == 0;
    }

private:
    int         mValue;
};
// DataStore.cpp

DataStore::DataStore() :
    mValue(0)
{}

DataStore::DataStore(int value) :
    mValue(value)
{
}

DataStore::DataStore(const DataStore& rhs) :
    mValue(rhs.mValue)
{
}

const DataStore DataStore::operator+(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue + rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator-(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue - rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator*(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue * rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator/(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue / rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator%(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue % rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator&(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue & rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator|(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue | rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator^(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue ^ rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator~() const
{
    DataStore tmp;
    tmp.mValue = ~mValue;
    return tmp;
}

const DataStore DataStore::operator<<(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue << rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator>>(const DataStore& rhs) const
{
    DataStore tmp;
    tmp.mValue = mValue >> rhs.mValue;
    return tmp;
}

const DataStore DataStore::operator-() const
{
    DataStore tmp;
    tmp.mValue = -mValue;
    return tmp;
}

const DataStore DataStore::operator++(int)
{
    const DataStore tmp = *this;
    ++(*this);
    return tmp;
}

const DataStore DataStore::operator--(int)
{
    const DataStore tmp = *this;
    --(*this);
    return tmp;
}

このように非常に多くの operator を定義しなければならないことがあります。数が多いのは単純に大変ですが、少なくて済む場合であっても、演算子ごとの実装方法の差異があるので注意して実装してください。

また、今回は練習という意味もあって、実装できそうなすべての演算子を定義しましたが、必要性の薄いと思われるものは、むしろ実装しないようにするべきです。クラスの利用者にとっては、使える機能が多すぎるクラスは、逆に使いづらいものです。


参考リンク


更新履歴

’2014/10/5 新規作成。



第19章のメインページへ

C++編のトップページへ

Programming Place Plus のトップページへ



はてなブックマーク に保存 Pocket に保存 Facebook でシェア
X で ポストフォロー LINE で送る noteで書く
rss1.0 取得ボタン RSS 管理者情報 プライバシーポリシー
先頭へ戻る