Reputation:
I have some problem when i want to make a "+" operator in my Derived class. I have already have a "+" oparator in my Base class.
class Base{
//Some data
public:
//Some other function
Base operator+(const Base& rhs_s) const ;
Base operator+(char rhs_c) const { return *this + Base(rhs_c);}
//Some other function
};
class Derived :public Base{
public:
Derived () :Base("") {}
Derived (char c) :Base(c) {}
Derived (const char* c) :Base(c) {}
Derived operator+(const Derived& s) const {
return Derived(*this) + Derived(s);
}
So I can't add two variable where "Derived + 'C'"
I thanks for the help
Upvotes: 0
Views: 69
Reputation: 1407
Using your code, I can do this fine:
Derived d{'c'};
const Derived d2 = d + 'C';
Your example in the comment is different:
const Base d("hello");
Derived b;
b = d + 'C';
The above code doesn't compile because will return a Base object, which cannot be implicitly upcasted to a Derived type. For example:
Derived d;
Base b = d; // This is fine because you are going from a more specific type to a less specific one
Derived d2 = b; // This is not okay unless you explicitly define an assignment/constructor for Derived that takes a Base object
Upvotes: 1