协变返回类型 [英] covariant return types

查看:145
本文介绍了协变返回类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个VectorN类和一个从VectorN继承的Vector3类(例如可以处理叉积)。我无法确定不同运算符的返回类型。示例:

I have a VectorN class, and a Vector3 class inherited from VectorN (which can handle cross products for example). I have trouble determining the return types of the different operators. Example:

class VectorN
{
public:
   VectorN(){};
   virtual VectorN operator*(const double& d) {.....};
   std::vector<double> coords;
};

class Vector3 : public VectorN
{
public:
  Vector3(){};
  virtual Vector3 operator*(const double& d) {....};
};

这个特定的例子产生一个C2555错误:
'Vector3 :: operator *':overriding虚函数返回类型不同,并且不是VectorN :: operator *的协变,参见VectorN :: operator *的声明。

This particular example produces a C2555 error : 'Vector3::operator *': overriding virtual function return type differs and is not covariant from 'VectorN::operator *', see declaration of 'VectorN::operator *'.

问题是,不返回对Vector3的引用,并且Vector3类未在运算符*的声明中完全定义。但是,我想我的操作符*是虚拟的,我想返回一个Vector3,当我将一个Vector3乘以一个常数(否则,如果我做(Vector3 * double).crossProduct(Vector3),它会返回一个错误。

The problem is that I don't return a reference to a Vector3, and that the Vector3 class is not fully defined at the declaration of the operator*. However, I want my operator* to be virtual, and I want to return a Vector3 when I multiply a Vector3 with a constant (otherwise, if I do (Vector3*double).crossProduct(Vector3), it would return an error).

我可以做什么?

谢谢!!

推荐答案

您需要重新设计。首先,喜欢自由功能超过成员功能。

You need a re-design. First, prefer free-functions over member-functions. The only member functions you should have are the ones that need access to privates.

开始使用此组合:

class VectorN
{
public:
   virtual VectorN& operator*=(double d)
    {
        /* ... */

        return *this;
    };
};


class Vector3 : public VectorN
{
public:
    virtual Vector3& operator*=(double d)
    {
        return static_cast<Vector3&>(VectorN::operator*=(d));
    };
};

这里协方差工作正常,因为类型是引用或指针,并且重新使用代码。 ( static_cast 是免费的,性能方面的,安全的,因为你知道派生类型。)

Here covariance works fine because the type is a reference or pointer, and you re-use code. (static_cast is free, performance-wise, and safe since you know the derived type.)

你的自由功能:

// optimization: if you're going to make a copy, do it in the parameter list;
// compilers can elide a copy when working with temporaries
VectorN operator*(VectorN v, double d)
{
    // reuse code
    return v *= d;
}

VectorN operator*(double d, VectorN v)
{
    // reuse code
    return v *= d;
}

使用 Vector3

所做的工作是,您可以使用轻松方式编写这些运算符,因为您可以使用运算符的核心和返回类型

What's been done is you get an easy way to write these operators because you get to use the core of the operator, and the return type matches, thanks to covariance.

不过,你可能不需要任何警告。您可以通过在向量 valarray 上操作的自由功能进行扩展。

Do heed warnings though, you probably don't need any of it. And extensions you want to make can be made via free-functions operating on a vector or valarray.

这篇关于协变返回类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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