继承和“无效的协变量返回类型";操作员重载错误 [英] inheritance and "invalid covariant return type" error on operator overloading

查看:116
本文介绍了继承和“无效的协变量返回类型";操作员重载错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下表示数值数组的类

I have the following class representing numerical arrays

class Array {

protected :
    double *data;   // this will hold the data of the array
    int     n;      // number of elements in the array

public :
    virtual Array operator+ ( double value ) const {
       // return a new array with the argument added to each element
    }

    // other declarations follow here...
};

和另一个继承自上一个类的类,并为每个元素添加一个布尔掩码

and another class inheriting from the previous one and adding a boolean mask for each element

class MaskedArray : public Array {

private : 
    bool *mask;   // this holds the boolean mask of the array

public :
    MaskedArray operator+ ( double value ) const {
       // return a new MaskedArray with the argument added to each element
    }

    // other declarations follow here...
}

当我尝试编译时,出现错误无效的协变量返回类型",这是正常现象,因为两个类中的两个重载运算符都具有相同的签名.

When I try to compile I get the error "invalid covariant return type", which is normal since the two overloaded operators in both classes have the same signature.

我能够通过引用而不是按值传递继承的类的重载运算符的参数来解决此问题,因为这会更改函数的签名,同时为两个类保留相同的接口.但是我觉得这不是很干净,如果我想从MaskedArray继承怎么办?我会面临同样的问题.

I was able to circumvent this problem by passing the argument of the overloaded operator of the inherited class by reference rather than by value, as this changes the signature of the function, while keeping the same interface for both classes. But I feel this is not very clean, and what if I wanted to inherit from MaskedArray ? I would face the same problem.

我希望能够在我的客户代码中写这样的东西

I want to be able to write stuff like this in my client code

Array array;
// populate array here
Array array2 = array + 1.0;

MaskedArray maskedArray;
// populate maskedArray here
MaskedArray maskedArray2 = maskedArray + 1.0

是否有比我的"hack"更优雅的方式来实现这一目标?

Is there another more elegant way than my 'hack' to achieve this ?

推荐答案

协变量意味着被覆盖的虚函数的返回类型继承自基类函数的返回类型.

Covariant means that the return type of an overridden virtual function is inherited from the return type of the base class function.

但是:尽管C ++通常支持协方差,但是该标准仅在返回指针或引用时才允许协方差. operator+按值返回 ,这就是为什么会出现编译器错误的原因.这不是特定于运算符,而是适用于每个功能.

But: Although covariance is generally supported by C++, the standard allows covariance only when returning pointers or references. operator+ returns by value, that's why you get the compiler error. That is not specific to operators but applies to every function.

删除virtual关键字将消除编译器错误.但是,如果要这样做,请记住,将operator+应用于实际引用MaskedArrayArray引用或指针将返回Array(因此不包含mask成员.)将其保存到MaskedArray,原始mask丢失).

Removing the virtual keyword will eleminate the compiler error. But if you do so, keep in mind that applying operator+ to an Array reference or pointer, which actually references a MaskedArray returns an Array (and thus does not contain the mask member. If you cast it to a MaskedArray the original mask is lost).

这篇关于继承和“无效的协变量返回类型";操作员重载错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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