operator= and functions that are not inherited in C++?

前端 未结 3 483
心在旅途
心在旅途 2020-11-27 16:02

Until a test I\'ve just made, I believed that only Constructors were not inherited in C++. But apparently, the assignment operator= is not too...

3条回答
  •  春和景丽
    2020-11-27 16:27

    The assignment operator is technically inherited; however, it is always hidden by an explicitly or implicitly defined assignment operator for the derived class (see comments below).

    (13.5.3 Assignment) 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 a class if not declared by the user, a base class assignment operator is always hidden by the copy assignment operator of the derived class.

    You can implement a dummy assignment operator which simply forwards the call to the base class operator=, like this:

    // Derived class
    template class Derived : public Base
    {
    public:
        template::value>::type>
        inline Derived& operator=(const T0& rhs)
        {
            return Base::operator=(rhs);
        }
    };
    

提交回复
热议问题