functions with same signature

Discussion in 'C++' started by John Goche, Oct 12, 2006.

  1. John Goche

    John Goche Guest

    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
    John Goche, Oct 12, 2006
    #1
    1. Advertising

  2. John Goche

    Sumit Rajan Guest

    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-lite/const-correctness.html#faq-18.12

    Regards,
    Sumit.
    Sumit Rajan, Oct 12, 2006
    #2
    1. Advertising

  3. John Goche

    Kai-Uwe Bux Guest

    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
    Kai-Uwe Bux, Oct 12, 2006
    #3
  4. John Goche

    Rolf Magnus Guest

    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.
    Rolf Magnus, Oct 12, 2006
    #4
  5. John Goche

    Ron Natalie Guest

    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).
    Ron Natalie, Oct 12, 2006
    #5
  6. John Goche

    Alex Buell Guest

    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?
    Alex Buell, Oct 19, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. JJBW
    Replies:
    1
    Views:
    10,041
    Joerg Jooss
    Apr 24, 2004
  2. Replies:
    1
    Views:
    3,729
    =?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
    Sep 19, 2005
  3. Replies:
    0
    Views:
    3,140
  4. sqweek
    Replies:
    2
    Views:
    390
    Charles Rapp
    Jan 13, 2006
  5. Gustavo Narea
    Replies:
    14
    Views:
    847
    Gustavo Narea
    Feb 16, 2009
Loading...

Share This Page