1

So I have these two overloaded operators for my Vector class, dot and cross product, I assume you cannot do this, and I should have a cross function instead.

inline T operator *(const Vector3<T> &v)
{
    return value[0]*v[0]+value[1]*v[1]+value[2]*v[2];
}

inline Vector3<T> operator *(const Vector3<T> &v)
{
    Vector3<T> result;
    result[0] = value[1]*v[2] - value[2]*v[1]; 
    result[1] = value[2]*v[0] - value[0]*v[2];
    result[2] = value[0]*v[1] - value[1]*v[0];
    return result;
}

In the off chance there is a way to do this that would be great, is it at all possible?

Baraphor
  • 249
  • 4
  • 16

2 Answers2

6

Short answer: Not possible

Long answer: You could do something like

template<class T>
class Product
{
public:
   Product (const Vector3<T> &v1, const Vector3<T> &v2)
       : v1_ (v1), v2_(v2) {}

   operator T () const {/*calc and return a dot product*/}
   operator Vector3<T> () const {/*calc and return a cross product*/}

private:
   const Vector3<T> &v1_;
   const Vector3<T> &v2_;
};

template<class T>
class Vector3
{
...
public:
    inline Product<T> operator *(const Vector3<T> &v)
    {
         return Product<T> (*this, v);
    }
};

// usage
Vector3<int> v1 = {...};
Vector3<int> v2 = {...};

int dot = v1 * v2;
Vector3<int> cross = v1 * v2;
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • That has a lot of errors, for example using "this" on a free operator overload. – imreal Oct 31 '12 at 18:14
  • And also use of `return return` inside the operator. – Remy Lebeau Oct 31 '12 at 18:17
  • it's not afree operator, it's defined in OP's Vector3 class –  Oct 31 '12 at 18:17
  • Outside the class? And how is that not overloading return type? You have an operator returning different types, try instantiating. – imreal Oct 31 '12 at 18:19
  • `Product::operator Vector ()` needs to use `Vector3` instead. – Remy Lebeau Oct 31 '12 at 18:19
  • @Nick, please read the question carafully and then my answer. Operators are defined inside the class. It just that they are posted separatly here for simplisity sake –  Oct 31 '12 at 18:21
  • If you define a member function/operator outside a class you need to give it context "Vector3::". I don't give down votes, but I am afraid this is wrong. – imreal Oct 31 '12 at 18:23
  • The basic idea is on the right track to show that this is possible—and the fact that the details are so wrong on the first attempt shows why you usually don't want to do things like this. (However, there are cases where this trick useful—Mozilla's XPCOM wrappers, Unicode conversion helper classes, etc..) – abarnert Oct 31 '12 at 18:29
  • @Nick: there was nothing wrong about it since it was implemented `inline` inside the `Vector3` class. I edited the answer to make it clearer. – Remy Lebeau Oct 31 '12 at 18:30
  • One note of caution here: Product class stores references to both operands. While this usually is not a problem I can imagine a situation where one or both of them are changed before sequence point and you can get unexpected results. It would be safer to store the copies of both operands in Product class and shouldn't cost a lot for this class (it may be a different story for classes with large allocations inside thou). – Tomek Oct 31 '12 at 18:53
5

You can't overload on return type.

You have a few choices:

  • Use free functions dot(v1, v2);
  • Abuse another binary operator that doesn't make sense for your class (e.g. v1 ^ v2) as the dot or cross operator (note that the precedence may not be what you want);
  • Abuse paired operators and objects to construct an appropriate syntax (e.g. v1 <dot> v2, using the < and > operators and a global dot object).
ecatmur
  • 152,476
  • 27
  • 293
  • 366
  • It's worth mentioning that there _is_ another possibility, using a helper class with conversion operators, but it's very verbose and tricky to get right. See the answer by @aleguna for more. – abarnert Oct 31 '12 at 18:30
  • +1 for the novel third point about `` syntax (even though as I see it it's pretty impractical, it's nice to learn a new trick). on the other hand, the list is missing the proxy-with-conversion answer, as in @aleguna's answer. while i wouldn't choose that for this problem, it does apply more generally, e.g. for standard C++ "properties". – Cheers and hth. - Alf Oct 31 '12 at 18:36