- NONconst-version was called - - NONconst-version was called - A(1,1)=8.8
- NONconst-version was called - Error: you may only alter the first row of the matrix.
나는 것을 달성 할 수있는 방법 :
#include <iostream>
#include <vector>
using namespace std;
class Matrix {
public:
Matrix(int m, int n) {
vector<double> tmp(m, 0.0);
data.resize(n, tmp);
}
~Matrix() { }
const double & operator()(int ii, int jj) const {
cout << " - const-version was called - ";
return data[ii][jj];
}
double & operator()(int ii, int jj) {
cout << " - NONconst-version was called - ";
if (ii!=1) {
throw "Error: you may only alter the first row of the matrix.";
}
return data[ii][jj];
}
protected:
vector< vector<double> > data;
};
int main() {
try {
Matrix A(10,10);
A(1,1) = 8.8;
cout << "A(1,1)=" << A(1,1) << endl;
cout << "A(2,2)=" << A(2,2) << endl;
double tmp = A(3,3);
} catch (const char* c) { cout << c << endl; }
}
이 나에게 다음과 같은 출력을 제공합니다 C++은 operator()의 const 버전을 호출합니까? 나는 GCC 4.4.0을 사용하고있다.
매트릭스 항목을 수정하고 수정할 수 있기를 원합니다. – steigers