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

C

Claudius

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;
}
int & operator [] ( int i ) {
cout << "int & operator[]() called" << endl;
return vec;
}
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
 
I

Ian Collins

Claudius said:
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.
 
B

Bo Persson

Claudius said:
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;
}
int & operator [] ( int i ) {
cout << "int & operator[]() called" << endl;
return vec;
}
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top