I have a template base class that implements void operator=(T value). Then I have a derived template class which should just inherit that operator, but this doesn't work. (See compile error in the code comment.) When I just rename the operator to void operator+=(T value) it works so there must be some magic going on with the assignment operator.
#include <iostream>
template<typename T>
class A
{
public:
void operator=(T value) {
_value = value;
}
T value() {
return _value;
}
void print() {
std::cout << "A: " << value() << std::endl;
}
private:
T _value;
};
template<typename T>
class B : public A<T>
{
public:
void print() {
std::cout << "B: " << this->value() << std::endl; // Why is "this->" needed?
}
};
int main(int, char**)
{
// Works just fine
A<int> a;
a = 4;
a.print();
// Doesn't compile
B<int> b;
b = 5; // error: no match for ‘operator=’ (operand types are ‘B<int>’ and ‘int’)
b.print();
return 0;
}
Edit: I know there are a lot answered questions like this. But they all use the operator= with the same signature that the default generated assignment operator would have. But I don't have this signature and so the explanations there do not directly apply for me.