I’m reading Essential C++. In the section 4.3, the author gives an example to
explain the usage of const
class val_class {
public:
val_class(const BigClass &v)
: _val(v) {}
const BigClass& val() const {return _val;}
BigClass& val() {return _val;}
};
void example(const BigClass *pbc, BigClass &rbc)
{
pbc->val();
rbc.val();
}
In the above code segment, the author overloads the function val based on constness.
And then the pbc->val() will invoke const instance, rbc.val() will invoke non-const instance.
The pbc is a const pointer of type BigClass. How can we guarantee that the BigClass has a member function val ? Should the BigClass be replaced with val_class in the example function?
Must be this: Your Full Example
This is obviously just a typo in the book. As you figured out for yourself, the
examplefunction should look as follows