Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > TC++PL example, hard to understand

Reply
Thread Tools

TC++PL example, hard to understand

 
 
torhu
Guest
Posts: n/a
 
      08-27-2003
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...

 
Reply With Quote
 
 
 
 
Thomas Matthews
Guest
Posts: n/a
 
      08-27-2003
torhu wrote:

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



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.


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


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:stream;

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

ostream& operator<<(ostream& 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:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      08-27-2003
On 27 Aug 2003 04:51:00 -0700, (torhu) wrote:

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


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




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


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



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


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


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


Yup.



>Is it just me being slow? I have pondered this problem for weeks
>now...


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


Hth.,

- Alf

 
Reply With Quote
 
Kevin Saff
Guest
Posts: n/a
 
      08-27-2003
"Thomas Matthews" <> wrote in message
news:jQ23b.34589$ gy.com...
> 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.


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.


 
Reply With Quote
 
torhu
Guest
Posts: n/a
 
      08-28-2003
I'll have a look at the errata list at Bjarne Stroustrup's homepage,
should have thought about that earlier...
 
Reply With Quote
 
Erik
Guest
Posts: n/a
 
      08-28-2003
> 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&);


This function is not there in my copy.

> double& operator[](string&);


But this one is.

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


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


 
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
program order: hard to understand definition pvbemmel-at-xs4all-nl Java 10 11-30-2010 04:08 PM
Hard to understand 'eval' TheSaint Python 10 06-16-2008 09:18 AM
why the perl documents is hard to understand? Xiaoshen Li Perl Misc 32 11-12-2005 08:57 PM
Read all of this to understand how it works. then check around on otherRead all of this to understand how it works. then check around on other thelisa martin Computer Support 2 08-18-2005 06:40 AM
Moving Mozilla files from old hard drive to new hard drive Nate Firefox 1 02-21-2004 09:09 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57