operand order in * operator overload

后端 未结 1 1725
轻奢々
轻奢々 2021-01-03 01:27

I\'m writing a vec3 class for my game physics engine.

I\'ve made an operator overload to allow me to multiply a vector by a scalar (to scale the vector):

<         


        
相关标签:
1条回答
  • 2021-01-03 01:49

    You need to declare a free operator, outside the class (or inside, as a friend):

    const vec3 operator*( const real n, vec3 v )
    {
        //no need to re-implement, since you already have v * n defined
        return v * n;
    }
    

    The reason is that, when declared as a class member, the first operator is implicitly this, so you basically have defined vec3 * real.

    0 讨论(0)
提交回复
热议问题