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

前端 未结 3 465
心在旅途
心在旅途 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<typename T, unsigned int N> class Derived : public Base<Derived, T, N>
    {
    public:
        template<typename T0, class = typename std::enable_if<std::is_convertible<T0, T>::value>::type>
        inline Derived& operator=(const T0& rhs)
        {
            return Base<Derived, T, N>::operator=(rhs);
        }
    };
    
    0 讨论(0)
  • 2020-11-27 16:29
    1. Your assignment operator is technically inherited, but then it's hidden by the default copy assignment operator in the derived class. This default copy assignment then tries to call the base class's copy assignment which doesn't exist since you hid it with your own assignment.

    2. The sanest way to resolve this is to not use operator overloading in non-obvious ways (= not meaning copy assignment for example). In this case, don't use operator=: Call it something like assign or set and then it will inherit and not be hidden by the child copy assignment.

    3. These operators are inherited and there are no compiler versions so they will never be automatically hidden like operator=.

    4. It really is only constructors that aren't inherited, and I can't think of any other compiler-generated functions that could hide something from the parent as in operator=.

    0 讨论(0)
  • 2020-11-27 16:32

    The assignment operator is inherited, sort of, but... In any given class, if you do not provide a copy assignment operator, the compiler generates one for you. That means that your derived classes effectively have an assignment operator:

    Derived& operator=( Derived const& );
    

    And the usual hiding rules apply; this hides all of the base class assignment operators. (If the base class had an assignment operator with this signature, the derived class would inherit it normally.)

    0 讨论(0)
提交回复
热议问题