[C ++]协变返回类型 [英] [C++]Covariant return types

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

问题描述

我有一个 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错误:

This particular example produces a C2555 error:

'Vector3 :: operator *':覆盖的虚函数返回类型与'VectorN :: operator *'不同并且不是协变的,请参见'VectorN :: operator *'的声明。

'Vector3::operator *': overriding virtual function return type differs and is not covariant from 'VectorN::operator *', see declaration of 'VectorN::operator *'.

问题是我没有返回对 Vector3 的引用, 运算符* 的声明中没有完全定义 Vector3 类。但是,我希望我的 operator * 是虚拟的,并且我想在乘以<$时返回 Vector3 c $ c> 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.)

然后您实现您的自由功能:

Then you implement your free-functions:

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

不过请注意警告,您可能不需要任何警告。而要创建的扩展可以通过在 vector 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.

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

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