Operator Overloading

Overloading With Different Types

Can we overload operators to work with different types? For example, could I multiply a Vector3 by a float instead of just an int?

Yes, you can absolutely overload operators to work with different types! This is a common and useful practice, especially for mathematical types like our Vector3. Let's implement multiplication with both int and float:

#include <iostream>

struct Vector3 {
  float x, y, z;

  // Multiply Vector3 by float
  Vector3 operator*(float scalar) const {
    return Vector3{
      x * scalar, y * scalar, z * scalar
    };
  }

  // Multiply Vector3 by int
  Vector3 operator*(int scalar) const {
    return Vector3{
      x * scalar, y * scalar, z * scalar
    };
  }
};

// Allow float * Vector3
Vector3 operator*(float scalar, const Vector3& vec) {
  return vec * scalar;  // Reuse the member operator
}

// Allow int * Vector3
Vector3 operator*(int scalar, const Vector3& vec) {
  return vec * scalar;  // Reuse the member operator
}

int main() {
  Vector3 vec{1.0f, 2.0f, 3.0f};

  // Using different numeric types
  Vector3 result1{vec * 2};     // Using int
  Vector3 result2{vec * 2.5f};  // Using float

  // Using float on the left side
  Vector3 result3{3.0f * vec};

  std::cout << "Result2: x=" << result2.x
    << ", y=" << result2.y
    << ", z=" << result2.z << "\n";
}
Result2: x=2.5, y=5, z=7.5

You can overload operators for any types that make sense in your context. Some other common examples include:

  • Multiplying a vector by a double for high-precision calculations
  • Adding a position vector to a direction vector
  • Multiplying a matrix by a vector
  • Adding a time duration to a timestamp

The key is to implement operators that make semantic sense - if the operation would make sense mathematically or logically, it's probably a good candidate for operator overloading.

Remember that when overloading operators with different types, you might need to consider both orderings (vector * scalar and scalar * vector) if you want the operation to work both ways.

This Question is from the Lesson:

Operator Overloading

This lesson introduces operator overloading, a fundamental concept to create more intuitive and readable code by customizing operators for user-defined types

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Operator Overloading

This lesson introduces operator overloading, a fundamental concept to create more intuitive and readable code by customizing operators for user-defined types

3D art showing a progammer setting up a development environment
Part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

This course includes:

  • 60 Lessons
  • Over 200 Quiz Questions
  • 95% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

View Course
Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2025 - All Rights Reserved