I’m having some trouble understanding this issue.
I have a class:
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;
}
};
This class (which have more methods and were snipped for simplicity) should act as a string.
When I derive from it:
class Test : public StringProperty { };
I want to do something like this:
Test x;
x = "test";
However, this fails miserably (does not compile):
error: no match for ‘operator=’ in ‘x = "test"’
Nonetheless, if I use
x.foo("test");
It works.
I’m interested in understanding why it fails, since for me both functions are identical.
Thanks.
Your
Testclass contains an implicitly-declared copy-assignment operator (and also a default constructor, copy constructor and destructor). This hides the one in the base class. In order for that to be considered as an overload, you have to make it accessible in the derived class: