Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > functions with same signature

Reply
Thread Tools

functions with same signature

 
 
John Goche
Guest
Posts: n/a
 
      10-12-2006

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

 
Reply With Quote
 
 
 
 
Sumit Rajan
Guest
Posts: n/a
 
      10-12-2006
John Goche wrote:
> 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-lit...html#faq-18.12

Regards,
Sumit.
 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      10-12-2006
John Goche wrote:

> 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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      10-12-2006
John Goche wrote:

>
> 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.

 
Reply With Quote
 
Ron Natalie
Guest
Posts: n/a
 
      10-12-2006
John Goche wrote:
> 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).
 
Reply With Quote
 
Alex Buell
Guest
Posts: n/a
 
      10-19-2006
On 12 Oct 2006 03:28:00 -0700, I waved a wand and this message
magically appears in front of John Goche:

> 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.
--
http://www.munted.org.uk

You've been eating the cat food again, haven't you?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Turning a signature-changing decorator into a signature-preservingone Gustavo Narea Python 14 02-16-2009 04:58 PM
REQ: how to override 2 virtual functions with the same name and same parameter zzppallas@gmail.com C++ 5 02-06-2006 06:54 AM
Constructors are having a special signature, different from ordinary member functions ? Razvan Java 1 09-30-2004 06:12 PM
please help me in distinguish redefining functions, overloading functions and overriding functions. Xiangliang Meng C++ 1 06-21-2004 03:11 AM
UTF-8 with signature & UTF-8 without signature JJBW ASP .Net 1 04-24-2004 10:21 AM



Advertisments