TC++PL example, hard to understand

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

    TC++PL example, hard to understand

    In 'The C++ Programming Language', Special Edition, there is an
    example in chapter 11.8, page 286 that is meant to illustrate the
    subscript operator (operator[]). It is a class that associates
    strings with doubles. It looks like this:

    class Assoc {
    public:
    const double& operator[](const string&);
    double& operator[](string&);

    //and some other stuff...
    };

    There are nother other operator[]'s declared. I have two questions:
    1. Why does one function return a const double& when the function
    itself is not declared const? What purpose does this serve?

    2. What is the point of using what looks to me as a non-const
    input-only argument in the second function?

    Why not like this:
    double& operator[](const string&);
    and maybe this too ?:
    const double& operator[](const string&) const;


    Is it just me being slow? I have pondered this problem for weeks
    now...
    :)
  • Thomas Matthews

    #2
    Re: TC++PL example, hard to understand

    torhu wrote:
    [color=blue]
    > In 'The C++ Programming Language', Special Edition, there is an
    > example in chapter 11.8, page 286 that is meant to illustrate the
    > subscript operator (operator[]). It is a class that associates
    > strings with doubles. It looks like this:
    >
    > class Assoc {
    > public:
    > const double& operator[](const string&);
    > double& operator[](string&);
    >
    > //and some other stuff...
    > };
    >
    > There are nother other operator[]'s declared. I have two questions:
    > 1. Why does one function return a const double& when the function
    > itself is not declared const? What purpose does this serve?[/color]


    Returning a "const double&" prevents the client from modifying the
    internal value. This has nothing to do with a function being const.
    A function declared as "const" promises not to modify any of the
    class' data members.

    [color=blue]
    > 2. What is the point of using what looks to me as a non-const
    > input-only argument in the second function?
    >
    > Why not like this:
    > double& operator[](const string&);
    > and maybe this too ?:
    > const double& operator[](const string&) const;
    >
    >
    > Is it just me being slow? I have pondered this problem for weeks
    > now...
    > :)[/color]

    The second function returns a reference to a value. Since it is
    returning a reference, the value inside the function can be
    modified by the client using the function.

    Try this:
    #include <iostream>
    using std::cout;
    using std::ostream;

    class Example_Class
    {
    double my_var;
    public:
    Example_Class(d ouble new_value)
    : my_var(new_valu e)
    { ; }
    double & paradox(void)
    {return my_var;}
    friend ostream& operator<<(ostr eam& out,
    const Example_Class& ec);
    };

    ostream& operator<<(ostr eam& out,
    const Example_Class& ec)
    {
    out << ec.my_var;
    return out;
    }

    int main(void)
    {
    Example_Class example(3.14159 );
    cout << "Original value: " << example << "\n";
    example.paradox () = 1.1414;
    cout << "After paradox(): " << example << "\n";
    return 0;
    }

    The key issue here is "references ". The method is not
    returning a _copy_ of the item, but a _reference_ to
    the item. Although references can be more convenient
    than passing copies, they do have their suprises as
    show above. To allow the convenience of not making
    a copy of the value and prevent the client from changing
    the value, the return type is a const reference.

    --
    Thomas Matthews

    C++ newsgroup welcome message:

    C++ Faq: http://www.parashift.com/c++-faq-lite
    C Faq: http://www.eskimo.com/~scs/c-faq/top.html
    alt.comp.lang.l earn.c-c++ faq:

    Other sites:
    http://www.josuttis.com -- C++ STL Library book

    Comment

    • Alf P. Steinbach

      #3
      Re: TC++PL example, hard to understand

      On 27 Aug 2003 04:51:00 -0700, thusaboe@hotmai l.com (torhu) wrote:
      [color=blue]
      >In 'The C++ Programming Language', Special Edition, there is an
      >example in chapter 11.8, page 286 that is meant to illustrate the
      >subscript operator (operator[]). It is a class that associates
      >strings with doubles. It looks like this:
      >
      >class Assoc {
      >public:
      > const double& operator[](const string&);
      > double& operator[](string&);
      >
      > //and some other stuff...
      >};
      >
      >There are nother other operator[]'s declared. I have two questions:
      >1. Why does one function return a const double& when the function
      >itself is not declared const? What purpose does this serve?[/color]

      _Probably_ the intent was to have a const function. Have you checked
      the book's errata list?



      [color=blue]
      >2. What is the point of using what looks to me as a non-const
      >input-only argument in the second function?[/color]

      That is more definitively an error. Have you checked the book's
      errata list?


      [color=blue]
      >Why not like this:
      > double& operator[](const string&);[/color]

      Judging from the limited information you've given, that seems to be
      much better, yes.

      [color=blue]
      >and maybe this too ?:
      > const double& operator[](const string&) const;[/color]

      Yup.


      [color=blue]
      >Is it just me being slow? I have pondered this problem for weeks
      >now...[/color]

      Impossible to say for sure without more context, but it _does_ look
      as oversights/errors in the book.


      Hth.,

      - Alf

      Comment

      • Kevin Saff

        #4
        Re: TC++PL example, hard to understand

        "Thomas Matthews" <Thomas_Matthew sHatesSpam@sbcg lobal.net> wrote in message
        news:jQ23b.3458 9$Vx2.15011787@ newssvr28.news. prodigy.com...[color=blue]
        > The key issue here is "references ". The method is not
        > returning a _copy_ of the item, but a _reference_ to
        > the item. Although references can be more convenient
        > than passing copies, they do have their suprises as
        > show above. To allow the convenience of not making
        > a copy of the value and prevent the client from changing
        > the value, the return type is a const reference.[/color]

        It's obvious he knows what a reference is. His point is one wouldn't expect
        that kind of declaration. How often have you seen:

        double const& operator[] (string const&);
        double& operator[] (string&);

        as opposed to (the probably intended):

        double const& operator[] (string const&) const;
        double& operator[] (string const&);



        It doesn't make much sense to selectively return a const or non-const
        reference based on whether the string parameter is const or non-const. If
        you can think of a good reason for doing so, please let us all know.


        Comment

        • torhu

          #5
          Re: TC++PL example, hard to understand

          I'll have a look at the errata list at Bjarne Stroustrup's homepage,
          should have thought about that earlier...

          Comment

          • Erik

            #6
            Re: TC++PL example, hard to understand

            > In 'The C++ Programming Language', Special Edition, there is an[color=blue]
            > example in chapter 11.8, page 286 that is meant to illustrate the
            > subscript operator (operator[]). It is a class that associates
            > strings with doubles. It looks like this:
            >
            > class Assoc {
            > public:
            > const double& operator[](const string&);[/color]

            This function is not there in my copy.
            [color=blue]
            > double& operator[](string&);[/color]

            But this one is.
            [color=blue]
            > //and some other stuff...
            > };
            >
            > There are nother other operator[]'s declared. I have two questions:
            > 1. Why does one function return a const double& when the function
            > itself is not declared const? What purpose does this serve?
            >
            > 2. What is the point of using what looks to me as a non-const
            > input-only argument in the second function?
            >
            > Why not like this:
            > double& operator[](const string&);
            > and maybe this too ?:
            > const double& operator[](const string&) const;
            >
            >
            > Is it just me being slow? I have pondered this problem for weeks
            > now...[/color]

            Since they don't seem logical and are not there in my copy, it is probably a
            typo,


            Comment

            Working...