Which approach is better?

Passing a user defined type by value is as expensive as returning a new one?
The second approach has code repetition, because operator/= do almost the same thing.
Which is better?

First approach
1
2
3
4
5
inline Vec3f operator/ (Vec3f v1, float scalar)
{
    return v1/= scalar;
}


Second approach

1
2
3
4
inline Vec3f operator /(const Vec3f& v1, float scalar)
{
    return Vec3f(v1.m_x / scalar, v1.m_y / scalar, v1.m_z / scalar);
}



The first one. One way or another, you need to construct a new object and fill it with (a multiple of) the dividend.
Last edited on
Thanks!
Topic archived. No new replies allowed.