functions with same signature

J

John Goche

Hello,

I have come across the following class functions:

T& operator[](int index) { return array[index]; }
const T& operator[](int index) const { return array[index]; }

However, I have noticed that they have the same signature.
I thought it was not possible to define two class functions
with the same signature. How can the compiler differentiate
between these two?

Thanks,

JG
 
S

Sumit Rajan

John said:
Hello,

I have come across the following class functions:

T& operator[](int index) { return array[index]; }
const T& operator[](int index) const { return array[index]; }

However, I have noticed that they have the same signature.
I thought it was not possible to define two class functions
with the same signature. How can the compiler differentiate
between these two?

See:
http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.12

Regards,
Sumit.
 
K

Kai-Uwe Bux

John said:
I have come across the following class functions:

T& operator[](int index) { return array[index]; }
const T& operator[](int index) const { return array[index]; }

However, I have noticed that they have the same signature.
I thought it was not possible to define two class functions
with the same signature. How can the compiler differentiate
between these two?

Both are member functions of a class X. So when you have an object x of type
X, and you do x[...], then the non-const version will be called. But if x
is of type const X, then x[...] resolves to the const version. Its the
constness or non-constness of the object x that determines which member
function is chosen for x[...].


Best

Kai-Uwe Bux
 
R

Rolf Magnus

John said:
Hello,

I have come across the following class functions:

T& operator[](int index) { return array[index]; }
const T& operator[](int index) const { return array[index]; }

However, I have noticed that they have the same signature.

They don't.
I thought it was not possible to define two class functions
with the same signature. How can the compiler differentiate
between these two?

The presence or absence of 'const' after a member function name is part of
its signature.
 
R

Ron Natalie

John said:
Hello,

I have come across the following class functions:

T& operator[](int index) { return array[index]; }
const T& operator[](int index) const { return array[index]; }

However, I have noticed that they have the same signature.
I thought it was not possible to define two class functions
with the same signature. How can the compiler differentiate
between these two?
They do not have the same signature. The presence of the
const after the parameters means that the implicit "this"
parameter in the member function call is const.

The non-const one is preferred when called on non-const
objects (since it avoids the conversion to const).
 
A

Alex Buell

I have come across the following class functions:

T& operator[](int index) { return array[index]; }
const T& operator[](int index) const { return array[index]; }

One's a const, the other isn't.
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top