Assignment operator not available in derived class

后端 未结 4 1317
甜味超标
甜味超标 2021-01-03 19:02

The assignment operator in base class does not seem to be available in derived class. Given this code:

#include 

class A{
    int value;
pub         


        
4条回答
  •  遥遥无期
    2021-01-03 19:16

    In order to make it work, you need to bring the operator= into B's scope:

    class B : public A
    {
    public:
    using A::operator=;
    };
    

    According to the standard [class.copy.assign/8]:

    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 (16.5.3).

    So, because the B::operator= has been implicitly declared, it has hidden A::operator=, which requires you to bring it into scope if you want to use it.

    Further quote from the standard [over.ass/1]

    An assignment operator shall be implemented by a non-static member function with exactly one parameter. Because a copy assignment operator operator= is implicitly declared for a class if not declared by the user (15.8), a base class assignment operator is always hidden by the copy assignment operator of the derived class.

    Emphasis is mine.

提交回复
热议问题