473,396 Members | 1,987 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,396 software developers and data experts.

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...
:)
Jul 19 '05 #1
5 2742
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::ostream;

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

Jul 19 '05 #2
On 27 Aug 2003 04:51:00 -0700, th******@hotmail.com (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

Jul 19 '05 #3
"Thomas Matthews" <Th**********************@sbcglobal.net> wrote in message
news:jQ**********************@newssvr28.news.prodi 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.
Jul 19 '05 #4
I'll have a look at the errata list at Bjarne Stroustrup's homepage,
should have thought about that earlier...
Jul 19 '05 #5
> 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,
Jul 19 '05 #6

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

Similar topics

42
by: ES Kim | last post by:
Here's a code fragment from TC++PL Special Edition, p291: void f1(T a) { T v; T* p = &v; p--; // please note (my comment) *p = a; // oops: 'p' out of range, uncaught ++p; *p = a; // ok
5
by: Wayne Shu | last post by:
Now I'm reading Stroustrup's The C++ Programming Language(Special Edition). In section 4.4 Integer Types, he has wrote that "Using an unsigned instead of an int to gain one more bit to represent...
3
by: Guofu Chen | last post by:
Hi, In TC++PL, Chapter 10, there is an exercise which ask us to modify the following program: #include <iostream> int main() { std::cout << "Hello, world!/n" ;
8
by: Wayne Shu | last post by:
Hi everyone, I am reading B.S. 's TC++PL (special edition). When I read chapter 11 Operator Overloading, I have two questions. 1. In subsection 11.2.2 paragraph 1, B.S. wrote "In particular,...
3
by: HL | last post by:
As the subject, I like the font he used for codes. It's not constant- width but better than constant-width fonts while presenting text. Netters, could you tell me the name of that font? I hope to...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.