Subclasses, overloading assignment operators?

If I have the following classes:

class TestA { public: const TestA &operator=(const int A){return *this;} }; class TestB : public TestA { public: //Inheritance? }; 

The question assumes that the TestA class and TestB class have exactly the same content in terms of variables: Is the assignment operator (or any other operator) inherited?

Is the following valid?

 class TestB : public TestA { public: using TestA::operator=; //Inheritance? }; 

If this is true, would that change?

+6
source share
4 answers

Assignment operators are hidden by default derived class (since the compiler always generates T& operator = () for any class T , if not specified). What makes the inherited operator = unusable.

Yes , when you specify them with the using keyword; they become useful . Demo So your code snippet makes sense.

 public: using TestA::operator=; 
+3
source

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: //Inheritance? }; 

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=; //Inheritance? }; 

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,

 // o = 42; // won't compile, because it hidden. 

and it will compile anyway,

 o = 42; // compiles fine with "using". 

Cheers and hth.,

+3
source

I found some related answer from the C ++ 11 standard:

12.8 Copying and moving class objects

Since the copy / move assignment operator is implicitly declared for the class, unless it is declared by the user, the copy / move assignment operator of the base class is always hidden by the corresponding assignment operator of the derived class (13.5.3). Use-declaration (7.3.3), which introduces an assignment operator from a base class with a parameter type, which may be a copy / move assignment operator type for a derived class, is not considered an explicit declaration of such an operator and does not suppress the implicit declaration of the derived class operator; the statement entered using the using declaration is hidden by the implicitly declared statement in the derived class.

+2
source

An assignment operator is not inherited by a derivative. If you use your overloaded assignment operator in its derivative from your base, you may encounter slicing: sizeof(base) != sizeof(derived)

0
source

Source: https://habr.com/ru/post/896779/


All Articles