C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Q: overloaded operator[](int)const: influence on what versio

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
Claudius
Guest





PostPosted: Sat Feb 25, 2006 3:06 am    Post subject: Q: overloaded operator[](int)const: influence on what versio Reply with quote



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
Back to top
Ian Collins
Guest





PostPosted: Sat Feb 25, 2006 3:06 am    Post subject: Re: Q: overloaded operator[](int)const: influence on what ve Reply with quote



Claudius wrote:
Quote:
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]


Quote:

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.

Quote:
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.
Back to top
Bo Persson
Guest





PostPosted: Sat Feb 25, 2006 10:06 am    Post subject: Re: overloaded operator[](int)const: influence on what versi Reply with quote



"Claudius" <Claudius.Schnoerr (AT) gmx (DOT) de> skrev i meddelandet
news:1140833361.407141.268750 (AT) p10g2000cwp (DOT) googlegroups.com...
Quote:
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.

Quote:
------------------------------------------------


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
Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.