I understand what the inline keyword does in C++ but am a bit confused where and where not to use it.

So I have my Vector3 class I made -

#pragma once #include <iostream> #include "cVector4.h" template <class T> class cVector3 { public: T x, y, z; // Constructors cVector3(void) : x(0), y(0), z(0) { } // Set all values to zero as default cVector3(T argX, T argY, T argZ) : x(argX), y(argY), z(argZ) { } cVector3(const cVector4<T> &vec4) : x(vec4.x), y(vec4.y), z(vec4.z) { } ~cVector3(void) { } // Destructor // Copy constructor cVector3 (const cVector3& other) : x (other.x), y (other.y), z (other.z) { } void operator = (const cVector3 &argVector) { x = argVector.x; y = argVector.y; z = argVector.z; } void operator = (const float& argFloat) { x = argFloat; y = argFloat; z = argFloat; } // Negative vector cVector3 operator - (void) const { return cVector3 (-x, -y, -z); } // Vector - Vector assignment operators void operator += (const cVector3 &argVector) { x += argVector.x; y += argVector.y; z += argVector.z; } void operator -= (const cVector3 &argVector) { x -= argVector.x; y -= argVector.y; z -= argVector.z; } void operator *= (const cVector3 &argVector) { x *= argVector.x; y *= argVector.y; z *= argVector.z; } void operator /= (const cVector3 &argVector) { x /= argVector.x; y /= argVector.y; z /= argVector.z; } // Bool == Operator bool operator == (const cVector3 &argVector) { if(x == argVector.x && y == argVector.y && z == argVector.z) { return true; } return false; } // Vector - Vector operators cVector3 operator + (const cVector3 &argVector) const { return cVector3(x + argVector.x, y + argVector.y, z + argVector.z); } cVector3 operator - (const cVector3 &argVector) const { return cVector3(x - argVector.x, y - argVector.y, z - argVector.z); } cVector3 operator * (const cVector3 &argVector) const { return cVector3(x * argVector.x, y * argVector.y, z * argVector.z); } cVector3 operator / (const cVector3 &argVector) const { return cVector3(x / argVector.x, y / argVector.y, z / argVector.z); } // Vector - Value assignment operators void operator += (T value) { x += value, y += value, z += value; } void operator -= (T value) { x -= value, y -= value, z -= value; } void operator *= (T value) { x *= value, y *= value, z *= value; } void operator /= (T value) { x /= value, y /= value, z /= value; } // Vector - Value operators cVector3 operator + (T value) const { return cVector3(x + value, y + value, z + value); } cVector3 operator - (T value) const { return cVector3(x - value, y - value, z - value); } cVector3 operator * (T value) const { return cVector3(x * value, y * value, z * value); } cVector3 operator / (T value) const { return cVector3(x / value, y / value, z / value); } // Dot product T Dot(const cVector3 &argVector) const { return x * argVector.x + y * argVector.y + z * argVector.z; } // Cross product cVector3 Cross(cVector3 &argVector) { return Vector3f( y * argVector.z - z * argVector.y, z * argVector.x - x * argVector.z, x * argVector.y - y * argVector.x); } // Returns the length squared T LengthSquared() { return x * x + y * y + z * z; } // Returns the length of the vector T Length() { return sqrt(LengthSquared()); } // Sets the length of the vector void SetLength (T vecLength) { Normalise(); *this *= vecLength; } // Normalises the vector (i.e makes it a unit vector) void Normalise() { T vecLength = Length(); if(vecLength != 0) { *this /= vecLength; } } void Zero() { x = 0; y = 0; z = 0; } // Prints the x, y and z to console void PrintVector() { std::cout<< x << ", " << y << ", " << z << std::endl; } // Convert to Vector4 cVector4<T> ToVector4() { return cVector4<T>(x, y, z, 1); } operator cVector4<T>() const // Convert to vec 4 { return cVector4<T>(x, y, z, 1); } }; // Types typedef cVector3 <float> Vector3f; typedef cVector3 <double> Vector3d; typedef cVector3 <int> Vector3i;

So I put the inline keyword infront of the first lot of operators -

// Vector - Vector assignment operators inline void operator += (const cVector3 &argVector) { x += argVector.x; y += argVector.y; z += argVector.z; } inline void operator -= (const cVector3 &argVector) { x -= argVector.x; y -= argVector.y; z -= argVector.z; } inline void operator *= (const cVector3 &argVector) { x *= argVector.x; y *= argVector.y; z *= argVector.z; } inline void operator /= (const cVector3 &argVector) { x /= argVector.x; y /= argVector.y; z /= argVector.z; }

which gave me a boost in the performance of my program so I thought "sweet I'll do this for all the operators" like so -

#pragma once #include <iostream> #include "cVector4.h" template <class T> class cVector3 { public: T x, y, z; // Constructors cVector3(void) : x(0), y(0), z(0) { } // Set all values to zero as default cVector3(T argX, T argY, T argZ) : x(argX), y(argY), z(argZ) { } cVector3(const cVector4<T> &vec4) : x(vec4.x), y(vec4.y), z(vec4.z) { } ~cVector3(void) { } // Destructor // Copy constructor cVector3 (const cVector3& other) : x (other.x), y (other.y), z (other.z) { } void operator = (const cVector3 &argVector) { x = argVector.x; y = argVector.y; z = argVector.z; } void operator = (const float& argFloat) { x = argFloat; y = argFloat; z = argFloat; } // Negative vector cVector3 operator - (void) const { return cVector3 (-x, -y, -z); } // Vector - Vector assignment operators inline void operator += (const cVector3 &argVector) { x += argVector.x; y += argVector.y; z += argVector.z; } inline void operator -= (const cVector3 &argVector) { x -= argVector.x; y -= argVector.y; z -= argVector.z; } inline void operator *= (const cVector3 &argVector) { x *= argVector.x; y *= argVector.y; z *= argVector.z; } inline void operator /= (const cVector3 &argVector) { x /= argVector.x; y /= argVector.y; z /= argVector.z; } // Bool == Operator inline bool operator == (const cVector3 &argVector) { if(x == argVector.x && y == argVector.y && z == argVector.z) { return true; } return false; } // Vector - Vector operators inline cVector3 operator + (const cVector3 &argVector) const { return cVector3(x + argVector.x, y + argVector.y, z + argVector.z); } inline cVector3 operator - (const cVector3 &argVector) const { return cVector3(x - argVector.x, y - argVector.y, z - argVector.z); } inline cVector3 operator * (const cVector3 &argVector) const { return cVector3(x * argVector.x, y * argVector.y, z * argVector.z); } inline cVector3 operator / (const cVector3 &argVector) const { return cVector3(x / argVector.x, y / argVector.y, z / argVector.z); } // Vector - Value assignment operators inline void operator += (T value) { x += value, y += value, z += value; } inline void operator -= (T value) { x -= value, y -= value, z -= value; } inline void operator *= (T value) { x *= value, y *= value, z *= value; } inline void operator /= (T value) { x /= value, y /= value, z /= value; } // Vector - Value operators inline cVector3 operator + (T value) const { return cVector3(x + value, y + value, z + value); } inline cVector3 operator - (T value) const { return cVector3(x - value, y - value, z - value); } inline cVector3 operator * (T value) const { return cVector3(x * value, y * value, z * value); } inline cVector3 operator / (T value) const { return cVector3(x / value, y / value, z / value); } // Dot product T Dot(const cVector3 &argVector) const { return x * argVector.x + y * argVector.y + z * argVector.z; } // Cross product cVector3 Cross(cVector3 &argVector) { return Vector3f( y * argVector.z - z * argVector.y, z * argVector.x - x * argVector.z, x * argVector.y - y * argVector.x); } // Returns the length squared T LengthSquared() { return x * x + y * y + z * z; } // Returns the length of the vector T Length() { return sqrt(LengthSquared()); } // Sets the length of the vector void SetLength (T vecLength) { Normalise(); *this *= vecLength; } // Normalises the vector (i.e makes it a unit vector) void Normalise() { T vecLength = Length(); if(vecLength != 0) { *this /= vecLength; } } void Zero() { x = 0; y = 0; z = 0; } // Prints the x, y and z to console void PrintVector() { std::cout<< x << ", " << y << ", " << z << std::endl; } // Convert to Vector4 cVector4<T> ToVector4() { return cVector4<T>(x, y, z, 1); } operator cVector4<T>() const // Convert to vec 4 { return cVector4<T>(x, y, z, 1); } }; // Types typedef cVector3 <float> Vector3f; typedef cVector3 <double> Vector3d; typedef cVector3 <int> Vector3i;

Is this correct, I know that the book I was reading said to be careful when using the inline keyword as it is easy to think that inlining everything with make your program run faster but it won't. If someone could advise roughly where and where not to use it would be helpful. Also should I use it on the other functions?

Many Thanks

x