I've got a VectorN class, along with a Vector3 class inherited from VectorN (which could handle mix items for instance). I've trouble identifying the return kinds of the various 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) {....};
};

This specific example creates a C2555 error : 'Vector3::operator *': overriding virtual function return type differs and isn't covariant from 'VectorN::operator *', see promise of 'VectorN::operator *'.

However , I do not return a mention of the a Vector3, which the Vector3 class isn't fully defined in the promise of the operator*. However, I would like my operator* to become virtual, and I wish to return a Vector3 after i multiply a Vector3 having a constant (otherwise, basically do (Vector3*double).crossProduct(Vector3), it might return a mistake).

So what can I actually do ?

Thanks!!

You'll need a re-design. First, prefer free-functions over member-functions. The only real member functions you ought to have are the type that require use of privates.

Begin with this combo:

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

Here covariance works fine since the type is really a reference or pointer, and also you re-use code. (static_cast is free of charge, performance-smart, and safe because you be aware of derived type.)

Then you definitely 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;
}

Perform the same goes with Vector3.

What's been done is that you simply have an easy method to write these operators because you're able to make use of the core from the operator, and also the return type matches, because of covariance.


Do heed alerts though, you most likely have no need for any one of it. And extensions you need to make can be created via free-functions operating on the vector or valarray.

You cannot possess a virtual function which has a different return key in its override. If a person calls the bottom class' implementation, wouldso would they are fully aware what kind to anticipate?

Btw, Hopefully this code sits dormant inside a graphics-heavy atmosphere where performance matters.

The very best I'm able to think about would be to replace the return type having a wise pointer and forgo covariance in support of polymorphism:

virtual auto_ptr< VectorN > operator*(const double& d);

The main reason I would recommend this is you are utilizing virtual functions, so understanding the exact kind of the item is not necessary anyway.

The actual problem would be that the caller must allocate storage to have an object came back by value. That storage cannot vary dynamically, which means you are inevitably stuck allocating the item around the heap.