M
Mark Stijnman
I would like to be able to have an object accessible as a vector using
the [] operator, but able to track modifications to its data, so that
it can update other internal data as needed. I figured that setting a
flag in the non-const operator[] would work, but it doesn't. Take the
code below:
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
class Test {
public:
Test() {
for (int i=0; i<4; i++) t = 1.0;
}
const double& operator[](const unsigned index) const {
cerr << "const operator called" << endl;
return t[index];
}
double& operator[](const unsigned index) {
cerr << "non-const operator called" << endl;
return t[index];
}
private:
double t[4];
};
int main() {
Test t;
t[2] = 2; // #1
cout << "t[2] = " << t[2] << endl; // #2
t[3] += t[2] + t[1]; // #3
cout << "t[3] = " << t[3] << endl; // #4
}
I personally expected it to use the non-const version in the lines
marked #1 and #3, where it uses t as an lvalue, and use the const
version when t is used as an rvalue, like in lines #2, the right
hand side of #3, and in line #4. Unfortunately, on my system, when run,
it appears only the non-const operator gets called. Apparantly that's
not how it works. How are the rules here defined? And how do I get the
behaviour I want (i.e., different versions called, depending on use as
an lvalue or not)?
regards Mark
the [] operator, but able to track modifications to its data, so that
it can update other internal data as needed. I figured that setting a
flag in the non-const operator[] would work, but it doesn't. Take the
code below:
#include <iostream>
using std::cout;
using std::cerr;
using std::endl;
class Test {
public:
Test() {
for (int i=0; i<4; i++) t = 1.0;
}
const double& operator[](const unsigned index) const {
cerr << "const operator called" << endl;
return t[index];
}
double& operator[](const unsigned index) {
cerr << "non-const operator called" << endl;
return t[index];
}
private:
double t[4];
};
int main() {
Test t;
t[2] = 2; // #1
cout << "t[2] = " << t[2] << endl; // #2
t[3] += t[2] + t[1]; // #3
cout << "t[3] = " << t[3] << endl; // #4
}
I personally expected it to use the non-const version in the lines
marked #1 and #3, where it uses t as an lvalue, and use the const
version when t is used as an rvalue, like in lines #2, the right
hand side of #3, and in line #4. Unfortunately, on my system, when run,
it appears only the non-const operator gets called. Apparantly that's
not how it works. How are the rules here defined? And how do I get the
behaviour I want (i.e., different versions called, depending on use as
an lvalue or not)?
regards Mark