Sunday, June 3, 2007

Item 3

use const whenerver possible:

const allows you to specify a semantic constraint and compilers enforce it for you.

if the word const appears to the left of the asterisk what is pointed to is constant
if it is on left than the pointer itself is constant.

const widget* ptr = widget const * prt

having a function return a constant value often makes it possible to reduce the incidence of client errors without giving up the safety or efficiency

e.g.
class Rational {...}
const Rational operator* (const Rational& a , const Rational & b);

this causes a compile error if clients were to say ?

Rational a ,b,cl
( a*b) = c ;

the above can be caused by a simple type in a comparison statement


one of the hallmarks of good user defined types is that they avoid gratuitous incompatiblities with the built ins.

const member functions make interface easier to understand as you know whether a given function can change the object or not.
They help efficiency as you can work with const& which are a way to improve efficiency in function calls taking uder objects as parameters.

functions differing only in their constness can be overloaded.e.g.

class TextBlock {
public:
const char& operator[] ( std::size_t position) const { return text[position];}
char& operator[] ( std::size_t position) const { return text[position];}

private std::string text;
};

TextBlock tb;
std::cout << tb[0]; ---second function called

const TextBlock ctb;
std::cout << ctb[0]; ---first function called

note that we need to return references otherwise constness doesnt make sense. since the retunr value will get copied and const becomes useless.

will the below compile ?

class TextBlock {
public:
const char operator[] ( std::size_t position) const { return text[position];}
char operator[] ( std::size_t position) const { return text[position];}

private std::string text;
};

constness = physical constness / logical constness


it says that this code compiles, whereas i think i have got an error when i tried to compile it in past:

class CTextBlock {
public:
char& operator[](std::size_t position) const
{ return pText[position]; }
private char* pText;
};

mutable frees the non static data members from the constraints of bitwise constness

avoid code duplication in const and non const member functions:
see if you can call the const version from the non const version and cast away the constness ( dont do other way round....since the non const function change something in the object passed to it)

---also make sure you dont get yourself into an infinite loop non const calling itself again and again

things to remember:
declaring something const helps compiler detect usage errors. const can be applied to objects at any scope, to function parameters and return types and to member functions as a whole.
compilers enforce bitwise constness, but you should program using conceptual constness
when const and non const member functions have essentially identical implementations code duplications can be avoided by having the non const version call the const version

No comments: