const overloading

T

thomas

#include<iostream>
using namespace std;

class foo{
int ar[30];
public:
const int& operator[](unsigned index) const{
cout<<"one"<<endl;
return ar[index];
}
int& operator[](unsigned index){
cout<<"two"<<endl;
return ar[index];
}
};

int main(){
foo f;
cout<<f[3]<<endl;
// f[3] = 3; //(1)
cout<<f[3]<<endl;

}
---------------------code----------------------
The FAQ 18.12 (http://www.parashift.com/c++-faq-lite/const-
correctness.html) gives a similar example to the above one.

I expected the "const" version is called since expression (1) is
commented.
But I found that only the non-const version is called whether (1) is
commented or not.

Do const overloading work in this case? Any more details?
 
R

Richard Herring

In message
#include<iostream>
using namespace std;

class foo{
int ar[30];
public:
const int& operator[](unsigned index) const{
cout<<"one"<<endl;
return ar[index];
}
int& operator[](unsigned index){
cout<<"two"<<endl;
return ar[index];
}
};

int main(){
foo f;
cout<<f[3]<<endl;
// f[3] = 3; //(1)
cout<<f[3]<<endl;

}
---------------------code----------------------
The FAQ 18.12 (http://www.parashift.com/c++-faq-lite/const-
correctness.html) gives a similar example to the above one.

I expected the "const" version is called since expression (1) is
commented.

Huh? "const" means (loosely) "cannot be modified", not "has not been
modified".
But I found that only the non-const version is called whether (1) is
commented or not.

Your program doesn't contain any instances of const foo or const foo &,
so why would it be?
Do const overloading work in this case? Any more details?

Try adding this, and see what happens:

foo const & g = f;
cout << g[3] << endl;
 
J

James Kanze

#include<iostream>
using namespace std;
class foo{
int ar[30];
public:
const int& operator[](unsigned index) const{
cout<<"one"<<endl;
return ar[index];
}
int& operator[](unsigned index){
cout<<"two"<<endl;
return ar[index];
}
};
int main(){
foo f;
cout<<f[3]<<endl;
// f[3] = 3; //(1)
cout<<f[3]<<endl;
}
---------------------code----------------------
The FAQ 18.12 (http://www.parashift.com/c++-faq-lite/const-
correctness.html) gives a similar example to the above one.
I expected the "const" version is called since expression (1)
is commented. But I found that only the non-const version is
called whether (1) is commented or not.
Do const overloading work in this case? Any more details?

Overload resolution occurs at compile time, independently of
whatever might have happened to the object before hand. With
very few exceptions (e.g. taking the address of a function), it
occurs independently of the context, based entirely on the
arguments of the function (or the operator). In the above,
you'll get the non-const operator if the instance of foo is
non-const, the const operator if it is const. Regardless of
what you do with the return value afterwards.
 

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,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top