Consider the example:
class Gfg {
public:
void printHello()
{
cout << "hello gfg-class specific" << endl;
}
};
void printHello()
{
cout << "hello gfg-global" << endl;
}
int main()
{
Gfg a;
a.printHello();
printHello();
}
This code works fine.
But see other example for operator overloading, it will not compile.
class Gfg {
public:
Gfg operator+(Gfg& a)
{
cout << "class specific + operator" << endl;
return Gfg(); // Just return some temporary object
}
};
Gfg operator+(Gfg& a, Gfg& b)
{
cout << "global + operator called" << endl;
return Gfg(); // Just return some temporary object
}
int main()
{
Gfg a, b;
Gfg c = a + b;
}
I thought in both cases name mangling(decoration) will make both name as unique name and member function will be given preference and hence member version of operator+ should be called, which doesn't happen. why?