/*重载()
只能用成员函数,不能友元
重载(),其函数调用格式一般为<基本表达式>(表达式)
可以看成是一个二元运算符“()”
设x是X类一个对象,则表达式为x(arg1,arg2)
可以解释为x.operator()(arg1,arg2)
以下举个例子:用以实现数学函数的抽象
*/
#include <iostream>
using namespace std;
class F
{
public:
double operator()(double x,double y);//使用运算符重载实现
double ff(double x,double y);//使用一般的普通成员函数实现
};
double F::operator()(double x,double y)
{
return x*y+5;
}
double F::ff(double x,double y)
{
return x*y+5;
}
void main()
{
F f;
cout<<"使用隐式调用:";
cout<<f(1,5)<<endl;
cout<<"使用显式调用:";
cout<<f.operator()(1,5)<<endl;
cout<<"------"<<"使用一般的成员函数来实现"<<"------"<<endl;
cout<<f.ff(1,5)<<endl;
}
只能用成员函数,不能友元
重载(),其函数调用格式一般为<基本表达式>(表达式)
可以看成是一个二元运算符“()”
设x是X类一个对象,则表达式为x(arg1,arg2)
可以解释为x.operator()(arg1,arg2)
以下举个例子:用以实现数学函数的抽象
*/
#include <iostream>
using namespace std;
class F
{
public:
double operator()(double x,double y);//使用运算符重载实现
double ff(double x,double y);//使用一般的普通成员函数实现
};
double F::operator()(double x,double y)
{
return x*y+5;
}
double F::ff(double x,double y)
{
return x*y+5;
}
void main()
{
F f;
cout<<"使用隐式调用:";
cout<<f(1,5)<<endl;
cout<<"使用显式调用:";
cout<<f.operator()(1,5)<<endl;
cout<<"------"<<"使用一般的成员函数来实现"<<"------"<<endl;
cout<<f.ff(1,5)<<endl;
}