473,387 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

functions with same signature


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

Oct 12 '06 #1
5 2301
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.
Oct 12 '06 #2
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

Oct 12 '06 #3
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.

Oct 12 '06 #4
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).
Oct 12 '06 #5
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?
Oct 19 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: David M. Wilson | last post by:
Further to my last post here, I was playing some more with building a regex object and storing it somewhere for use internally by a function. I'm not happy with any of my solutions: # I don't...
3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
0
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and...
5
by: Angus Leeming | last post by:
Dinkumware's online STL reference http://tinyurl.com/3es52 declares std::map's overloaded erase member functions to have the interface: map::erase iterator erase(iterator where); iterator...
9
by: Jon Wilson | last post by:
I have a class which needs to accumulate data. The way we get this data is by calling a member function which returns float on a number of different objects of different type (they are all the...
2
by: Bryan Olson | last post by:
The current Python standard library provides two cryptographic hash functions: MD5 and SHA-1 . The authors of MD5 originally stated: It is conjectured that it is computationally infeasible to...
3
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
23
by: Timothy Madden | last post by:
Hello all. I program C++ since a lot of time now and I still don't know this simple thing: what's the problem with local functions so they are not part of C++ ? There surely are many people...
8
by: Edward Diener | last post by:
By reuse, I mean a function in an assembly which is called in another assembly. By a mixed-mode function I mean a function whose signature has one or more CLR types and one or more non-CLR...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.