operator difference

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • stef

    #1

    operator difference

    Hello,
    Could you tell me the difference between

    char &operator[](int idx)
    {
    return tab[idx];
    }

    and

    char operator[](int idx)
    {
    return tab[idx];
    }



    Thanks...
  • Victor Bazarov

    #2
    Re: operator difference

    stef wrote:
    Could you tell me the difference between
    >
    char &operator[](int idx)
    {
    return tab[idx];
    }
    >
    and
    >
    char operator[](int idx)
    {
    return tab[idx];
    }
    The sixth symbol, '&' (called "ampersand" ). Makes the return
    value type different.

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask


    Comment

    • Tomás Ó hÉilidhe

      #3
      Re: operator difference

      "Victor Bazarov" <v.Abazarov@com Acast.netwrote in comp.lang.c++:
      stef wrote:
      >Could you tell me the difference between
      >>
      > char &operator[](int idx)
      > {
      > return tab[idx];
      > }
      >>
      >and
      >>
      > char operator[](int idx)
      > {
      > return tab[idx];
      > }

      The one that returns a reference can be used as an L-value (in an
      assignment for instance).

      my_obj[5] = 77;

      --
      Tomás Ó hÉilidhe

      Comment

      • Abhishek Padmanabh

        #4
        Re: operator difference

        On Dec 13, 10:47 pm, stef <stef.pellegr.. .@gmail.comwrot e:
        Hello,
        Could you tell me the difference between
        >
        char &operator[](int idx)
        {
        return tab[idx];
        }
        >
        and
        >
        char operator[](int idx)
        {
        return tab[idx];
        }
        If they were members of a class - they would cause overload ambiguity.
        You would declare the second one as const member, and first one as non-
        const. That is how random access is implemented for std::vector/
        std::string etc. Of course, for them the argument idx is not a signed
        int. It would be their specific size_type, for example: implementation
        defined typedef for an unsigned integral type.

        The second one returns by value, so a copy is returned.

        Comment

        Working...