- 论坛徽章:
- 0
|
在c++ FAQs中看到的一段代码
为什么一般都只调用返回引用的重载函数,另一个在什么时候调用?
#include <stdexcept>
using namespace std;
class Matrix {
public:
double& operator() (unsigned row, unsigned col)
throw(out_of_range);
double operator() (unsigned row, unsigned col) const
throw(out_of_range);
private:
enum { rows_ = 100, cols_ = 100 };
double data_[rows_ * cols_];
};
inline double& Matrix::operator() (unsigned row, unsigned col)
throw(out_of_range)
{
if (row >= rows_ || col >= cols_)
throw out_of_range("Matrix::operator()");
return data_[cols_*row + col];
}
inline double Matrix::operator() (unsigned row, unsigned col) const
throw(out_of_range)
{
if (row >= rows_ || col >= cols_)
throw out_of_range("Matrix::operator()");
return data_[cols_*row + col];
}
int main()
{
Matrix m;
m(5,8) = 106.15;
cout << m(5,8);
}
|
[ 本帖最后由 sww12081234 于 2008-1-18 22:42 编辑 ] |
|