Operator Overloading

Operators Across Different Types

Can I use operator overloading with custom types from different classes? Like adding a Vector3 to a Point3D?

Yes, you can overload operators to work with different custom types! This is particularly useful when you have related types that need to interact. Here's a practical example:

#include <iostream>

struct Vector3 {
  float x, y, z;
};

struct Point3D {
  float x, y, z;

  // Add a vector to this point to get a new point
  Point3D operator+(const Vector3& vec) const {
    return Point3D{x + vec.x, y + vec.y, z + vec.z};
  }
};

// Free function to handle vector + point
Point3D operator+(
  const Vector3& vec, const Point3D& point
) {
  return point + vec;  // Reuse the member operator
}

int main() {
  Point3D startPos{0.0f, 0.0f, 0.0f};
  Vector3 movement{1.0f, 2.0f, 3.0f};

  // Both operations work!
  Point3D pos1{startPos + movement};
  Point3D pos2{movement + startPos};

  std::cout << "New position: "
    << pos1.x << ", "
    << pos1.y << ", "
    << pos1.z << "\n";
}
New position: 1, 2, 3

Some key considerations when working with different types:

  • Make sure the operation makes semantic sense (e.g., Point + Vector = Point)
  • Consider implementing both orderings if commutative
  • Be clear about which type owns the operator implementation
  • Think about type conversion implications

Here's another example showing vector projection:

struct Vector3 {
  float x, y, z;

  // Project this vector onto another vector
  float operator|(const Vector3& other) const {
    return (
      x * other.x +
      y * other.y +
      z * other.z
    ) / (
      other.x * other.x +
      other.y * other.y +
      other.z * other.z
    );
  }
};

Remember that while you can overload operators between different types, you should ensure the operations make mathematical or logical sense in your domain.

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