title: Basic operator overloading syntax in c++
body: <p>I’m having a hard time forming a question of what I’m trying to ask so let me give you an example:</p>
  
  <p>Say we are working on a 3d system where we have defined a vector class, Vec3. We overload some arithmetic operators for obvious resons. Among these we overload the *operator so that it will return the dot product of two vectors. Now our code should look something like this:</p>
  
  <pre><code>class Vec3{
  private:
      float x, y, z;
  public:
      float operator*(const Vec3&amp;) const; //dot product
  .
  .
  .
  </code></pre>
  
  <p>Now say that we want to be able to scale our vector by using the *operator with something, say a float. This could be done by declaring the following:</p>
  
  <pre><code>    Vec3 operator*(const float) const;
      friend Vec3 operator*(const float, const Vec3&amp;);
  </code></pre>
  
  <p>This yeilds two overloads and <strong>I’m wondering if there’s a way to do it with just one</strong>, i.e. say we declare this instead of the above two lines:</p>
  
  <pre><code>    friend Vec3 operator*(const Vec3&amp;, const Vec3&amp;);
  </code></pre>
  
  <p>and then add default values for the Vec3 ctor to handle the convertion from float.</p>
  
  <p>This last example would work for:</p>
  
  <pre><code>    Vec3 someVec3 = 1.0 * otherVec3; //through friend Vec3 operator*(const float, const Vec3&amp;)
  </code></pre>
  
  <p>however not for this:</p>
  
  <pre><code>    Vec3 someVec3 = otherVec3 * 1.0;
  </code></pre>
  
  <p>as the compiler wouldn’t know which of the two to use:</p>
  
  <pre><code>    friend Vec3 operator*(const float, const Vec3&amp;); //the one we want to use
  or
      float operator*(const Vec3&amp;) const; //the one for the dot product
  </code></pre>
  
  <p>Any suggestions?</p>
  
  <p>Thank you!</p>