2016-06-14 2 views
0

私はBaseクラスと生成されたコードを再利用するためにプライベート継承を使用するSomethingという名前の "子"クラスを持っています。C++のプライベート継承と、再利用するが基本クラスの演算子を飾る方法は?

class Base { 
protected: 
    Base(); 
public: 
    virtual ~Base(){} 
    void cool(); 
    Base& operator=(const Base& other); 
}; 
class Something : private Base {  
public: 
    Something(); 
    void cool(){ 
      // own stuff 
      // call method from base 
      Base::cool(); 
      // own stuff 
    } 

    Something& operator=(const Something& other){ 
      if (this != &other){ 
       // do own things 

ここでは装飾の間に、演算子の呼び出し=基本クラスからする必要があります。 しかし、私は正しい方法でこれをどうやってやるべきかわかりません。

   (dynamic_cast<Base>(*this)).operator =(dynamic_cast<Base>(other)); 
       // do own things 
      } 
      return *this; 
    } 

} 

敬具:私はこのような dynamic_castのものを使用する必要があります。

+0

ない 'this->ベース::演算子は=(その他)'働くだろうか? – melpomene

+1

'Base :: operator =(other)' – Arunmu

答えて

2

private継承を使用してもかかわらず、SomethingはまだBaseあるので、それを直接呼び出すことが許可されているBaseさんとoperator =()渡されたSomething

ので:Base::operator =(other);

関連する問題