Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sub-classes, assignment operator overloading?

Tags:

c++

subclass

If I have the given below classes:

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

class TestB : public TestA
{
    public:
        //Inheritance?
};

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?

like image 964
SSight3 Avatar asked Sep 07 '11 10:09

SSight3


2 Answers

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=;
like image 148
iammilind Avatar answered Sep 26 '22 23:09

iammilind


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.,

like image 28
Cheers and hth. - Alf Avatar answered Sep 25 '22 23:09

Cheers and hth. - Alf