const and non-const operator[] Vs. lvalue and rvalue use



 DEVELOP > c-Plus-Plus > const and non-const operator[] Vs. lvalue and rvalue use

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
Topic: DEVELOP > c-Plus-Plus
User: "Mark Stijnman"
Date: 22 Apr 2005 08:52:59 AM
Object: const and non-const operator[] Vs. lvalue and rvalue use
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[i] = 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[i] as an lvalue, and use the const
version when t[i] 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
.

User: "=?ISO-8859-15?Q?Juli=E1n?= Albo"

Title: Re: const and non-const operator[] Vs. lvalue and rvalue use 22 Apr 2005 09:32:37 AM
Mark Stijnman wrote:

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

You can return some type of proxy object in the non const operator [ ], and
in this object have an operator = that sets the modified flag corresponding
to the item it refers to.
--
Salu2
.

User: "Victor Bazarov"

Title: Re: const and non-const operator[] Vs. lvalue and rvalue use 22 Apr 2005 09:03:45 AM
Mark Stijnman wrote:

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[i] = 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[i] as an lvalue, and use the const
version when t[i] 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?

That's extremely simple. 't' (for which operator[] is called) is not
a const object. For a non-const object a non-const version is called.

And how do I get the
behaviour I want (i.e., different versions called, depending on use as
an lvalue or not)?

You can't (not easily, anyway). The return value type is not the deciding
factor in the process of picking which overloaded function to call.
V
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER