开发者

Sub-classes, assignment operator overloading?

If I have the given below classes:

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

class TestB : public TestA
{
    public:
        //Inh开发者_如何学运维eritance?
};

The question presumes both class TestA and class TestB have exactly the same contents 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 it is valid, would it make a difference?


Assignment operators are hidden by derived class by default (as compiler always generates a T& operator = () for any class T, if not specified). Which makes the inherited operator = not usable.

Yes when you specify them with using keyword; they become usable. Demo. So your code snippet does make sense.

public: using TestA::operator=;


The question's example code as I'm writing 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?"

Yes, of course. The only member functions that are not inherited in C++98, are constructors. However, the base class implementations are by default hidden by the automatically generated 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's hidden.
    cout << "(should be output here ->) ";  o.TestA::operator=( 42 );   // OK.
    cout << endl;
}

Considering all the answers so far that have confused hiding and inheritance, and just to clear up the terminology here, N3290 (which is identical to C++11 standard), says this:

N3290 §10/2:
"Inherited members can be referred to in expressions in the same manner as other members of the derived class, unless their names are hidden or ambiguous"

In the example code above TestA::operator= is hidden, but see the answer to Q3.

Q2: Is the following valid?

This question refers to use of using in the derived class, like

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

Yes, that is valid.

It would not be valid in C++98 for a constructor, because constructors are not inherited.

Q3: If it is valid, would it make a difference?

Yes, it makes the base class assignment operator(s) directly accessible in the derived class.

For example, you can then remove the out-commenting in the example above,

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

and it will still compile,

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

Cheers & hth.,


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

12.8 Copying and moving class objects

Because a copy/move assignment operator is implicitly declared for a class if not declared by the user, a base class copy/move assignment operator is always hidden by the corresponding assignment operator of a derived class (13.5.3). A using-declaration (7.3.3) that brings in from a base class an assignment operator with a parameter type that could be that of a copy/move assignment operator for the 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 operator introduced by the using-declaration is hidden by the implicitly-declared operator in the derived class.


Assignment operator isn't inherited by derived. If you use your overloaded assignment operator in your derived, from your base, you might encounter slicing: sizeof(base) != sizeof(derived)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜