~/Blog

Brandon Rozek

Photo of Brandon Rozek

PhD Student @ RPI studying Automated Reasoning in AI and Linux Enthusiast.

C++ Overloads

Published on

Updated on

2 minute reading time

Warning: This post has not been modified for over 2 years. For technical posts, make sure that it is still relevant.

One thing I like about a lot of common languages like C++ and Python is that you can overload operators. This makes it easy to create libraries for other developers that are easy to use. We are going to explain common operators you may want to overload in C++.

Quick Example

Let’s create a class

class DataType {
    public:
      DataType() {}
      bool operator==(const DataType&, const DataType&);
    private:
      int y;
}

Notice that we’re going to overload the == operator for this DataType.

bool operator==(const DataType &x1, const DataType &x2) {
    return x1.y == x2.y;
}

Common Unary Operators

Name Operator
Increment ++
Decrement --
Function Call ()
Boolean Conversion (bool) object
class DataType {
  public:
    DataType() {}
    // Boolean Conversion
    bool operator bool(const DataType&);
    // (In/De)crements
    DataType& operator++(DataType&);
    DataType& operator--(DataType&);
    // Function Call operator
    void operator(DataType& x)(int);
  private:
    int y;
}
// Boolean Conversion
bool operator bool(const DataType& x) {
    return x.y == 0;
}
// (In/De)crements
DataType& operator++(DataType& x) {
    x.y++;
    return x;
}
DataType& operator--(DataType& x) {
    x.y--;
    return x;
}
// Function Call Operator
void operator(DataType& x)(int n) { 
    x.y += n; 
}

Common Binary Operators

Name Operator
Stream Insertion <<
Stream Extraction >>
Less than <
Equals ==
Subscript []

Implementing < gives you >,<=, and >= for free.

Implementing == gives you != for free.

class DataType {
  public:
    DataType() {}
    DataType(int x) { y += x }
    // Stream Operators
    std::ostream& operator<<(std::ostream&, const DataType&);
    std::istream& operator>>(std::istream&, DataType&);
    // Arithmetic Operators
    DataType& operator+=(DataType&, const DataType&);
    DataType operator+(const DataType&, const DataType&);
    // Comparison Operators
    bool operator<(const DataType&, const DataType&);
    bool operator==(const DataType&, const DataType&);
    // Array Subscript
    int operator[](DataType&, std::size_t);
  private:
    int y;
}
// Stream Operators
std::ostream& operator<<(std::ostream& os, const DataType& x) {
    os << x.y;
    return os;
}
std::istream& operator>>(std::istream& is, DataType& x) {
    int y;
    is >> y;
    x.y = y;
    return is;
}
// Artithmetic Operators
DataType& operator+=(DataType& x, const DataType& z) {
    x.y += z.y;
    return x;
}
DataType operator+(const DataType& x, const DataType& z) {
    return DataType(x.y + z.y);
}
// Comparison Operators
bool operator<(const DataType &x, const DataType &z) {
    return x.y < z.y;
}
bool operator==(const DataType &x, const DataType &z) {
    return x.y == z.y;
}
// Array Subscript
int operator[](DataType& x, std::size_t idx) {
    return x.y;
}
Reply via Email Buy me a Coffee
Was this useful? Feel free to share: Hacker News Reddit Twitter

Published a response to this? :