Assignment operator not available in derived class

后端 未结 4 1315
甜味超标
甜味超标 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:25

    The problem is that the compiler will add an implicit assignment operator for the B class, declared as

    B& operator=(const B&);
    

    This operator will hide the operator from A, so the compiler will not know about it.

    The solution is to tell the compiler to also use the operator from A with the using keyword:

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

提交回复
热议问题