operator =和在C ++中不继承的函数? [英] operator= and functions that are not inherited in C++?

查看:159
本文介绍了operator =和在C ++中不继承的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

直到我刚才做的测试,我相信只有构造函数不是在C ++中继承的。但显然,赋值 operator = 不是太...

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


  1. 什么是原因是什么?

  2. 是否有任何解决方法来继承赋值运算符?

  3. operator + = operator - = ...?

  4. 是否继承所有其他函数(除了constructors / operator =)?

  1. What is the reason of that ?
  2. Is there any workaround to inherit the assignment operator ?
  3. Is it also the case for operator+=, operator-=, ... ?
  4. Are all other functions (apart from constructors/operator=) inherited ?

,我遇到这个问题,因为我在做一些CRTP:

In fact, I encountered this problem as I was doing some CRTP :

template<class Crtp> class Base
{
    inline Crtp& operator=(const Base<Crtp>& rhs) {/*SOMETHING*/; return static_cast<Crtp&>(*this);}
};

class Derived1 : public Base<Derived1>
{
};

class Derived2 : public Base<Derived2>
{
};

是否有任何解决方案可以使这项工作?

Is there any solution to get that working ?

编辑:好的,我已经孤立的问题。为什么以下是不工作?如何解决问题?

EDIT : OK, I have isolated the problem. Why the following isn't working ? How to solve the problem ?

#include <iostream>
#include <type_traits>

// Base class
template<template<typename, unsigned int> class CRTP, typename T, unsigned int N> class Base
{
    // Cast to base
    public:
        inline Base<CRTP, T, N>& operator()()
        {
            return *this;
        }

    // Operator =
    public:
        template<typename T0, class = typename std::enable_if<std::is_convertible<T0, T>::value>::type>
        inline CRTP<T, N>& operator=(const T0& rhs)
        {
            for (unsigned int i = 0; i < N; ++i) {
                _data[i] = rhs;
            }
            return static_cast<CRTP<T, N>&>(*this);
        }

    // Data members
    protected:
        T _data[N];
};

// Derived class
template<typename T, unsigned int N> class Derived : public Base<Derived, T, N>
{
};

// Main
int main()
{
    Derived<double, 3> x;
    x() = 3; // <- This is OK
    x = 3;   // <- error: no match for 'operator=' in ' x=3 '
    return 0;
}


推荐答案

赋值运算符在技术上是继承的;但是,它总是被派生类的显式或隐式定义的赋值运算符隐藏(见下面的注释)。

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


赋值)赋值运算符应该由一个
非静态成员函数来实现,它只有一个参数。因为如果用户未声明
,则为aa类隐式声明复制
赋值运算符 operator = ,基类赋值运算符总是
被派生类的副本赋值操作符隐藏。

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

你可以实现一个虚拟赋值操作符,基类 operator = ,如下所示:

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);
    }
};

这篇关于operator =和在C ++中不继承的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆