Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision | Next revision Both sides next revision | ||
programming:improve-your-cpp [2006/12/14 14:41] cyril |
programming:improve-your-cpp [2006/12/14 14:42] cyril |
||
---|---|---|---|
Line 10: | Line 10: | ||
==== Item 1 : Use const and inline instead of #define ==== | ==== Item 1 : Use const and inline instead of #define ==== | ||
Because constants don't appear in the symbole table which is annoying for debugging : | Because constants don't appear in the symbole table which is annoying for debugging : | ||
- | < | + | < |
const float ASPECT_RATIO = 1.653; | const float ASPECT_RATIO = 1.653; | ||
</ | </ | ||
And because there are lot of traps with macros : | And because there are lot of traps with macros : | ||
- | < | + | < |
template< | template< | ||
</ | </ | ||
Line 20: | Line 20: | ||
==== Item 2 : Prefer iostream to stdio.h ==== | ==== Item 2 : Prefer iostream to stdio.h ==== | ||
Because of type safety an extensiblity. To define stream operators in your class : | Because of type safety an extensiblity. To define stream operators in your class : | ||
- | < | + | < |
friend ostream& | friend ostream& | ||
ostream& | ostream& | ||
Line 45: | Line 45: | ||
==== Item 7 : Check the return value of new ==== | ==== Item 7 : Check the return value of new ==== | ||
Set an error-handling function, globally : | Set an error-handling function, globally : | ||
- | < | + | < |
void noMoreMemory() { cerr << " | void noMoreMemory() { cerr << " | ||
main() { set_new_handler(noMoreMemory); | main() { set_new_handler(noMoreMemory); | ||
</ | </ | ||
Or for a specific class : | Or for a specific class : | ||
- | < | + | < |
typedef void (*PEHF)(); // PEHF = pointer to error handling function | typedef void (*PEHF)(); // PEHF = pointer to error handling function | ||
Line 84: | Line 84: | ||
==== Item 8 : Adhere to convention when writing new ==== | ==== Item 8 : Adhere to convention when writing new ==== | ||
Means having the right return value (pointer or 0), and calling an error-handling function when insufficient memory is avalaible : | Means having the right return value (pointer or 0), and calling an error-handling function when insufficient memory is avalaible : | ||
- | < | + | < |
void * operator new(size_t size) // your operator new could take additional params | void * operator new(size_t size) // your operator new could take additional params | ||
{ | { | ||
Line 99: | Line 99: | ||
</ | </ | ||
If the operator new is in a class X, you should add before to attempt to allocate memory : | If the operator new is in a class X, you should add before to attempt to allocate memory : | ||
- | < | + | < |
if (size != sizeof(X)) return ::new char[size]; | if (size != sizeof(X)) return ::new char[size]; | ||
</ | </ | ||
Line 106: | Line 106: | ||
==== Item 9 : Avoid hiding the global new ==== | ==== Item 9 : Avoid hiding the global new ==== | ||
If you add parameters to your '' | If you add parameters to your '' | ||
- | < | + | < |
class X | class X | ||
{ | { | ||
Line 121: | Line 121: | ||
==== Item 10 : Write delete if you write new ==== | ==== Item 10 : Write delete if you write new ==== | ||
You can rewrite '' | You can rewrite '' | ||
- | < | + | < |
class Airplane | class Airplane | ||
{ | { | ||
Line 176: | Line 176: | ||
==== Item 15 : Have operator= return a reference to *this ==== | ==== Item 15 : Have operator= return a reference to *this ==== | ||
To allow chains assignments : | To allow chains assignments : | ||
- | < | + | < |
C& C:: | C& C:: | ||
</ | </ | ||
Line 269: | Line 269: | ||
'' | '' | ||
'' | '' | ||
- | < | + | < |
void update(X *px); | void update(X *px); | ||
const X x; | const X x; | ||
Line 276: | Line 276: | ||
'' | '' | ||
'' | '' | ||
- | < | + | < |
typedef void (*FuncPtr)(); | typedef void (*FuncPtr)(); | ||
FuncPtr funcPtrArray[10]; | FuncPtr funcPtrArray[10]; |