473,386 Members | 1,602 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,386 software developers and data experts.

Q: overloaded operator[](int)const: influence on what version is used?

Hello,

I have written a class A with the access operator[](int) overloaded by
a
A-const version which returns an int by value:
------------------------------------------------
#include <iostream>

using namespace std;

class A {
public:
A() {}

int operator [] ( int i ) const {
cout << "int operator[]() called" << endl;
return vec[i];
}
int & operator [] ( int i ) {
cout << "int & operator[]() called" << endl;
return vec[i];
}
private:
int vec[10];
};
------------------------------------------------

However, the compiler uses the non const version even if only an
int-value is read:
------------------------------------------------
A aObj;
int a;

a = aObj[5]; //int & operator []() is used
------------------------------------------------
Is it possible to recognize if the compiler can use the const-version?

I'd like to write a sparse data type, where the []-operator is used in
a
cascaded way, e.g. aObj[5][6][7]. If it was possible to recognize that
a
value is only read rather than written, the use of an access operator
could return null immediatedly in the case of a missed index.

Claudius

Feb 25 '06 #1
2 2866
Claudius wrote:
Hello,

I have written a class A with the access operator[](int) overloaded by
a
A-const version which returns an int by value:
------------------------------------------------ [snip]

Is it possible to recognize if the compiler can use the const-version?
The constness applies to the object, so if you have a const A, the const
operator will be used.
I'd like to write a sparse data type, where the []-operator is used in
a
cascaded way, e.g. aObj[5][6][7]. If it was possible to recognize that
a
value is only read rather than written, the use of an access operator
could return null immediatedly in the case of a missed index.

You can't use const as a means of determining read or write access. You
have to return a proxy object with assignment and conversion operators
from operator[] and use those.

--
Ian Collins.
Feb 25 '06 #2

"Claudius" <Cl***************@gmx.de> skrev i meddelandet
news:11**********************@p10g2000cwp.googlegr oups.com...
Hello,

I have written a class A with the access operator[](int) overloaded
by
a
A-const version which returns an int by value:
------------------------------------------------
#include <iostream>

using namespace std;

class A {
public:
A() {}

int operator [] ( int i ) const {
cout << "int operator[]() called" << endl;
return vec[i];
}
int & operator [] ( int i ) {
cout << "int & operator[]() called" << endl;
return vec[i];
}
private:
int vec[10];
};
------------------------------------------------

However, the compiler uses the non const version even if only an
int-value is read:
------------------------------------------------
A aObj;
int a;

a = aObj[5]; //int & operator []() is used
The operator used depends on the object. If aObj is const, the const
version is used. Otherwise the non-const.
------------------------------------------------
Is it possible to recognize if the compiler can use the
const-version?


The compiler cannot know what you intend to do with the return value.
You might save the reference, and do the update later.

Consider this:

int& aref = aObj[5]; // saves the reference, not the value

// lots of other code

aref = 42; // the update comes here!
Bo Persson
Feb 25 '06 #3

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

Similar topics

4
by: Alex Vinokur | last post by:
Why is it ambiguous? ------ foo.cpp ------ struct Foo { Foo operator* (Foo) { return Foo(); } Foo operator* (int) const { return Foo(); } Foo () {} Foo (int) {} };
15
by: Tim Clacy | last post by:
Please illuminate; what operator of class 'Event' will get matched for these two cases : Event ev1; Event ev2; // Case 1 // if (ev1) ;
0
by: Pedro | last post by:
Hello pythonians! ;-D , I have a little problem when I expose (assisted by boost.python) classes with virtual functions, specially with operator(). In the C++ code below I test two different...
4
by: Lycan. Mao.. | last post by:
Hello, I'm trying to write a function adapter object, but it fails with the above information. Can you help me. template <typename _Predicate> struct Unary_negate { typedef typename...
14
by: Jonas.Holmsten | last post by:
Hello I'm porting some C++ stuff to C and having problem to get it through gcc. Here is a condensed version of the problem: void foo(const int * const * const ptr) {} main()
4
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
5
by: Chris Forone | last post by:
hello group, why i cant make the InputIterator of the following func-temp const? template<typename InputIterator, typename OutputIterator> OutputIterator Types::Copy(InputIterator source,...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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,...

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.