42 lines
601 B
C++
42 lines
601 B
C++
// () 函数调用重载
|
||
// 当类对象作为函数调用时,会执行 operator()(参数列表) 函数
|
||
//
|
||
#include <iostream>
|
||
#include <cstring>
|
||
#include <cstdlib>
|
||
|
||
using namespace std;
|
||
|
||
class A
|
||
{
|
||
private:
|
||
int x, y;
|
||
|
||
public:
|
||
A(int x, int y) : x(x), y(y) {}
|
||
|
||
public:
|
||
void operator()(int a, int b)
|
||
{
|
||
this->x += a;
|
||
this->y += b;
|
||
show();
|
||
}
|
||
|
||
void show()
|
||
{
|
||
cout << x << ", " << y << endl;
|
||
}
|
||
};
|
||
|
||
int main()
|
||
{
|
||
A a1(10, 9);
|
||
a1.show();
|
||
|
||
a1(2, 3); // 类对象作为函数使用,实现 2+x,3+y
|
||
// a1.show();
|
||
|
||
return 0;
|
||
}
|