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