この問題を理解する上で問題があります。仮想課題
私はクラスを持っている:
class StringProperty { //snipped...
protected:
std::string s;
public:
virtual StringProperty& operator= (const std::string &x) {
s = x;
return *this;
}
virtual StringProperty& foo(const std::string &x) {
s = x;
return *this;
}
};
(複数のメソッドを持っており、簡単にするために切り取られた)このクラスは、文字列として行動しなければなりません。
私はそれから派生:私はこのような何かやりたい
class Test : public StringProperty { };
:
Test x;
x = "test";
はしかし、これは(コンパイルされません)、無残に失敗します。
error: no match for ‘operator=’ in ‘x = "test"’
それでも、私が使用する場合
x.foo("test");
これは機能します。 私は両方の機能が同一であるため、なぜ失敗するのかを理解することに興味があります。
ありがとうございました。
'StringProperty&operator =(...)'の戻り型は、 'x =" test ";のLHS型と互換性がありません。 – Kashyap
@thekashyap:オペレータは任意の型を返すことも、何も返すこともできません。連鎖を許すために '* this'を返すのは慣例に過ぎません。 –
http://stackoverflow.com/questions/3410688/c-inheritance-and-operator-overloadingの重複 – jjlin