#include <iostream>
using namespace std;
class operatorer{
public:
/*等号操作符重载*/
int operator()(int m_a, int m_b)
{
return (m_a*m_a+m_b*m_b);
}
private:
int a;
int b;
};
int main()
{
operatorer oop1;
int result = oop1(5, 10);
cout << result << endl;
system("pause");
return 0;
}
注意:括号操作符与函数调用很相似,但却不是同一类操作
例如:
#include <iostream>
using namespace std;
class operatorer{
public:
/*等号操作符重载*/
int operator()(int m_a, int m_b)
{
return (m_a*m_a + m_b*m_b);
}
private:
int a;
int b;
};
int main()
{
operatorer oop1;
int result = oop1(5, 10);
cout << result << endl;
system("pause");
return 0;
}
这里的函数调用的格式是:对象名.成员函数名(初始值),而在上述例子中的格式为对象名(初始值);