2017-03-06 6 views
0

私は派生クラスの仮想関数で次のコードを持っています。基底クラスを変更するために基底クラス内で同じ関数を呼び出すにはどうすればよいですか?仮想関数内の基底クラスを変更する

class Base{ 
public: 
    int a; 
    virtual Base & operator +=(Base const & rhs) 
    { 
    a += rhs.a; 
    return *this; 
    } 
}; 

class Derived: public Base{ 
public: 
    int b; 
    virtual Derived & operator +=(Derived const & rhs) 
    { 
    // What should I write to invoke the += in Base class? 
    // something like Base::+=(rhs.Base); 
    b += rhs.b; 
    return *this; 
    } 
}; 

答えて

4

あなたは追加することができます。

Base::operator+=(rhs); 

をベースバージョンを呼び出すこと。

+0

これは意味があります。しかし、なぜ 'Base'に' + = 'という関数がないので、なぜ' operator'という言葉を省略してBase :: + = rhs.Base; – Allanqunzi

+0

@Allanqunziと書くことができないのだろうか? '*(static_cast (this))+ = rhs;'と書くことができますが、それは良いとは思いません:) – mpiatek

+0

@Allanqunzi演算子の構文はそのようには機能しません –

関連する問題