Ker*_* SB 11
重载函数调用操作符:
struct polynomial
{
double a, b;
polynomial(double m, double n) : a(m), b(n) { } // represents "a * x + b"
double operator()(double x) const
{
return a * x + b;
}
};
Run Code Online (Sandbox Code Playgroud)
用法:
polynomial p(2.5, 3.8);
double val = p(1.0);
Run Code Online (Sandbox Code Playgroud)
通过重载,operator()
您可以像调用函数一样"调用"对象:
struct polynomial {
int operator()(int x)
{
/* calculate */
}
};
int main()
{
polynomial p;
int x = p(5);
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
111 次 |
最近记录: |