473,396 Members | 1,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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

Jul 23 '05 #1
2 2573
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
Jul 23 '05 #2
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
Jul 23 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: lothar | last post by:
re: 4.2.1 Regular Expression Syntax http://docs.python.org/lib/re-syntax.html *?, +?, ?? Adding "?" after the qualifier makes it perform the match in non-greedy or minimal fashion; as few...
3
by: Mario | last post by:
Hello, I couldn't find a solution to the following problem (tried google and dejanews), maybe I'm using the wrong keywords? Is there a way to open a file (a linux fifo pipe actually) in...
25
by: Yves Glodt | last post by:
Hello, if I do this: for row in sqlsth: ________pkcolumns.append(row.strip()) ________etc without a prior:
32
by: Adrian Herscu | last post by:
Hi all, In which circumstances it is appropriate to declare methods as non-virtual? Thanx, Adrian.
22
by: Steve - DND | last post by:
We're currently doing some tests to determine the performance of static vs non-static functions, and we're coming up with some odd(in our opinion) results. We used a very simple setup. One class...
8
by: Bern McCarty | last post by:
Is it at all possible to leverage mixed-mode assemblies from AppDomains other than the default AppDomain? Is there any means at all of doing this? Mixed-mode is incredibly convenient, but if I...
11
by: ypjofficial | last post by:
Hello All, So far I have been reading that in case of a polymorphic class ( having at least one virtual function in it), the virtual function call get resolved at run time and during that the...
2
by: Ian825 | last post by:
I need help writing a function for a program that is based upon the various operations of a matrix and I keep getting a "non-aggregate type" error. My guess is that I need to dereference my...
0
by: amitvps | last post by:
Secure Socket Layer is very important and useful for any web application but it brings some problems too with itself. Handling navigation between secure and non-secure pages is one of the cumbersome...
399
by: =?UTF-8?B?Ik1hcnRpbiB2LiBMw7Z3aXMi?= | last post by:
PEP 1 specifies that PEP authors need to collect feedback from the community. As the author of PEP 3131, I'd like to encourage comments to the PEP included below, either here (comp.lang.python), or...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.