0

I know in C++ how to define a method of a class overloading an operator, and I would like to know if I can define a function (not a method and not even a friend function of a class) using as function name. For example, operator==.

Obviously, such a function will take two parameters of the same type and will return a Boolean.

I tried, for example, to define this silly function:

char operator*(char C1, char C2){
  return ';';
}

And the g++ compiler answers with:

error: overloaded 'operator*' must have at least one parameter of class or enumeration type

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 3
    _"overloaded 'operator*' must have at least one parameter of class or enumeration type"_ - pretty clear, isn't it? Do you understand what the compiler is telling you? You can't overload operators for built-in types. – Lukas-T May 16 '21 at 17:05
  • Yes, it is possible, bu you cannot do that for primitive types like `char`. One of the arguments must be a `class` or `struct` or `enum` or (not sure about that) `union`. – Yksisarvinen May 16 '21 at 17:05
  • make a struct containing a single char and use that struct in place of char. – Abel May 16 '21 at 18:16
  • @Abel and if that struct has a constructor taking char, you might even get implicit conversion to work in your favor. – Mark Ransom May 16 '21 at 19:15
  • and an operator to char to get implicit conversion out. – Abel May 16 '21 at 19:19
  • Does this answer your question? [The General Syntax of operator overloading in C++](https://stackoverflow.com/questions/4421706/operator-overloading-in-c/4421715#4421715) – JaMiT May 17 '21 at 03:08

1 Answers1

0

As you can see in cppreference, you can define many types of operator overloading as a function (not method) like below:

#include <iostream>
 
class Fraction
{
    int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
    int n, d;
public:
    Fraction(int n, int d = 1) : n(n/gcd(n, d)), d(d/gcd(n, d)) { }
    int num() const { return n; }
    int den() const { return d; }
    Fraction& operator*=(const Fraction& rhs)
    {
        int new_n = n * rhs.n/gcd(n * rhs.n, d * rhs.d);
        d = d * rhs.d/gcd(n * rhs.n, d * rhs.d);
        n = new_n;
        return *this;
    }
};

std::ostream& operator<<(std::ostream& out, const Fraction& f)
{
   return out << f.num() << '/' << f.den() ;
}

bool operator==(const Fraction& lhs, const Fraction& rhs)
{
    return lhs.num() == rhs.num() && lhs.den() == rhs.den();
}

bool operator!=(const Fraction& lhs, const Fraction& rhs)
{
    return !(lhs == rhs);
}

Fraction operator*(Fraction lhs, const Fraction& rhs)
{
    return lhs *= rhs;
}
 
int main()
{
   Fraction f1(3, 8), f2(1, 2), f3(10, 2);
   std::cout << f1 << " * " << f2 << " = " << f1 * f2 << '\n'
             << f2 << " * " << f3 << " = " << f2 * f3 << '\n'
             <<  2 << " * " << f1 << " = " <<  2 * f1 << '\n';
}

And some operator must be defined as a method, like operator=. If you want to get more detail, you can see the C++ standard or some references like cppreference.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
sorosh_sabz
  • 2,356
  • 2
  • 32
  • 53