Example code of the question when I write this:
class TestA { public: const TestA &operator=(const int A){return *this;} }; class TestB : public TestA { public:
Q1: "Is the assignment operator (or any other operator) inherited?"
Oh sure. Constructors are the only member functions that are not inherited in C ++ 98. However, the base class implementation is hidden by default by the automatically created copy assignment operator. Example:
#include <iostream> using namespace std; class TestA { public: TestA const& operator=( int const ) { cout << "TestA = int" << endl; return *this; } }; class TestB : public TestA { public: // Automatically generated copy assignment operator. }; int main() { TestB o; cout << "Calling automatically generated copy assignment:" << endl; cout << "(should be nothing here ->) "; o = TestB(); cout << endl; cout << endl; cout << "Calling base class assignment operator:" << endl; // o = 42; // won't compile, because it hidden. cout << "(should be output here ->) "; o.TestA::operator=( 42 ); // OK. cout << endl; }
Given all the answers so far that have confused hiding and inheritance, and just to clarify the terminology here, N3290 (which is identical to the C ++ 11 standard) says the following:
N3290 Β§10 / 2:
"Inherited members can be referenced in expressions in the same way as other members of a derived class if their names are not hidden or ambiguous."
In the above example, the code TestA::operator= is hidden, but see the answer to Q3.
Q2: Is it valid?
This question relates to using using in a derived class, e.g.
class TestB : public TestA { public: using TestA::operator=;
Yes it really is.
This is not valid in C ++ 98 for a constructor because constructors are not inherited.
Q3: If this is true, would it change?
Yes, it makes base class assignment operators available directly in a derived class.
For example, you can remove out-commenting in the above example,
and it will compile anyway,
o = 42; // compiles fine with "using".
Cheers and hth.,