1、函数调用运算符 () 可以被重载用于类的对象。当重载 () 时,您不是创造了一种新的调用函数的方式,相反地,这是创建一个可以传递任意数目参数的运算符函数。
下面的实例演示了如何重载函数调用运算符 ()。
#include <iostream>
using namespace std;
class Distance
{
private:
int feet; // 0 到无穷
int inches; // 0 到 12
public:
// 所需的构造函数
Distance(){
feet = 0;
inches = 0;
}
Distance(int f, int i){
feet = f;
inches = i;
}
// 重载函数调用运算符
Distance operator()(int a, int b, int c)
{
Distance D;
// 进行随机计算
D.feet = a + c + 10;
D.inches = b + c + 100 ;
return D;
}
// 显示距离的方法
void displayDistance()
{
cout << "F: " << feet << " I:" << inches << endl;
}
};
int main()
{
Distance D1(11, 10), D2;
cout << "First Distance : ";
D1.displayDistance();
D2 = D1(10, 10, 10); // invoke operator()
cout << "Second Distance :";
D2.displayDistance();
return 0;
}
当上面的代码被编译和执行时,它会产生下列结果:
First Distance : F: 11 I:10
Second Distance :F: 30 I:120
2、
总结:
需要以类成员函数的形式对函数调用操作符“()”进行重载。
只有常成员函数才能处理常对象,故我们依然在类中提供两个版本的函数调用操作符重载函数。若调用对象为常对象(const),则必须用常函数。
这里面的关于在函数中抛出异常,在调用处捕捉异常,值得参考学习。
返回类型 operator()( 参数列表 );
例1:
#include<iostream>
#include<string>
using namespace std;
class Array
{
public:
Array(){len1 = 0; len2 = 0; num = NULL; };
Array(int m, int n);
int & operator()(int, int);
const int & operator()(int, int)const;
int getlen1()const {return len1;}
int getlen2()const {return len2;}
private:
int len1;
int len2;
int * num;
};
Array::Array(int m, int n)
{
int size = m * n;
try
{
num = new int[size];
}
catch(bad_alloc)
{
cerr<<"allocate storage failure!"<<endl;
throw;
}
len1 = m;
len2 = n;
}
int & Array::operator()(int i, int j)
{
if(i < 0 || i >= len1)
throw string("1 out of bounds!");
if(j < 0 || j >= len2)
throw string("2 out of bounds!");
return num[ i*len2 + j ];
}
const int & Array::operator()(int i, int j)const
{
if(i < 0 || i >= len1)
throw string("1 out of bounds!");
if(j < 0 || j >= len2)
throw string("2 out of bounds!");
return num[ i*len2 + j ];
}
int main()
{
Array A(3,4);
int i,j;
for(i = 0; i < A.getlen1(); i++)
for(j = 0; j < A.getlen2(); j++)
A(i,j) = i * A.getlen2() + j;
for(i = 0; i < A.getlen1(); i++)
for(j = 0; j < A.getlen2(); j++)
cout<< A(i,j)<<" ";
cout<<endl;
try
{
cout<< A(5, 3) << endl;
}
catch(string s)
{
cerr<< s <<endl;
}
try
{
cout<< A(2, 6) << endl;
}
catch(string s)
{
cerr<< s <<endl;
}
return 0;
}
当我们用A(i,j)的形式访问二维数组中的数据时,A(i,j)会调用类中的函数调用操作符重载函数,此时A(i,j)可以理解为:
A.operator()(i, j)
例1中程序运行结果如下:
0 1 2 3 4 5 6 7 8 9 10 11
1 out of bounds!
2 out of bounds!
在例1中的主函数中异常捕获语句,我们先运行的是A(5, 3),故而是第一个边界越界了,因此先抛出“1 out of bounds!”的异常,而后又运行A(2, 6),此时为第二个边界越界,抛出“2 out of bounds!”的异常。