Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Systematic approach of how to think about operator overloading

I’m learning how to do operator overloading and trying to come up with a systematic approach of how to know number of arguments an overloaded operator should take in and if the function should be constant.

I know the system is not perfect and doesn’t catch all the edge cases, but I’m thinking that will work itself out with some logical thinking in the end.

What I’m looking for is if this systematic approach is catching the majority of cases, or am I missing something important?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Systematic approach

Important: Think logically through the entire process regarding if the input paramters can be const, * or &.

  1. Is the operator overloader a member function and NOT a stream? It should take 1 argument which is the value/object to the right ➡️ of the operator, UNLESS it is a unary operator then it should take 0 arguments.
    Does it alter the class object? If it does, add const to the end of it.
  2. Is the operator overloader a member function and ALSO a stream? It should take 2 arguments, first a reference to the stream object and secondly (most likely) a reference to the class object.
    Does it alter the class object? If it does, add const to the end of it.
  3. Is the operator overloader NOT a member function? In that case it should take 2 arguments which is the value/object to the left ⬅️and right ➡️ of the operator, UNLESS it is a unary operator then it should take 1 argument. No const at the end needed.

Below is the header file that I based the system on.

Vector.h

#pragma once

#include <iostream>

class Vector
{
public:

  Vector(double x = 0.0, double y = 0.0);
  
  Vector& operator+=(Vector const& other);
  Vector& operator-=(Vector const& other);
  Vector& operator*=(double other);
  Vector& operator/=(double other);

  Vector operator-() const;
  
  bool operator==(Vector const& other) const;
  bool operator!=(Vector const& other) const;

  double operator*(Vector const& rhs) const;
  
  double length() const;

  friend std::ostream& operator<<(std::ostream& os, Vector const& other);
  friend std::istream& operator>>(std::istream& is, Vector& other);
  
private:
  
  double x;
  double y;
};

Vector operator+(Vector const& lhs, Vector const& rhs);
Vector operator-(Vector const& lhs, Vector const& rhs);
Vector operator*(Vector const& lhs, double rhs);
Vector operator*(double lhs, Vector const& rhs);
Vector operator/(Vector const& lhs, double rhs);

>Solution :

You got one thing wrong:

friend std::ostream& operator<<(std::ostream& os, Vector const& other);

You say this is written with two explicit arguments "because one of them is a stream." But actually it takes two explicit arguments because the friend keyword on the front implies this is a free function, not a member function. So it’s covered by your third rule, and your second rule should be deleted entirely.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading