mercredi 15 juillet 2020

C++ global operator method

I am not able to understand why I cannot be able to create global operator () method as like:

#include <iostream>
class A
{
    public:
        bool operator()(const A& a)
       {
           return true;
       }
};

bool operator()(int a, int b)
{
    return true;
}

int main ()
{
std::cout << "Hello\n";
}

When I compile the code I get below error:

$ c++ -std=c++14 a.cpp
a.cpp:11:29: error: 'bool operator()(int, int)' must be a nonstatic member function
 bool operator()(int a, int b)
                             ^

If I change the operator to any like + or - I still get the same error? Why we cannot defined global operator method?

Aucun commentaire:

Enregistrer un commentaire