Differences

This shows you the differences between two versions of the page.

Link to this comparison view

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 :
-<code c++>+<code cpp>
 const float ASPECT_RATIO = 1.653; const float ASPECT_RATIO = 1.653;
 </code> </code>
 And because there are lot of traps with macros : And because there are lot of traps with macros :
-<code c++>+<code cpp>
 template<class T> inline T& MAX(T& a, T& b) { return a>b ? a : b; } template<class T> inline T& MAX(T& a, T& b) { return a>b ? a : b; }
 </code> </code>
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 :
-<code c++>+<code cpp>
 friend ostream& operator<<(ostream& s, const ComplexInt& c); friend ostream& operator<<(ostream& s, const ComplexInt& c);
 ostream& operator<<(ostream& s, const ComplexInt& c) { s << c.r << " " << c.i; return s; } ostream& operator<<(ostream& s, const ComplexInt& c) { s << c.r << " " << c.i; return s; }
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 :
-<code c++>+<code cpp>
 void noMoreMemory() { cerr << "Unable to satisfy request for memory\n"; abort(); } void noMoreMemory() { cerr << "Unable to satisfy request for memory\n"; abort(); }
 main() { set_new_handler(noMoreMemory); ... } main() { set_new_handler(noMoreMemory); ... }
 </code> </code>
 Or for a specific class : Or for a specific class :
-<code c++>+<code cpp>
 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 :
-<code c++>+<code cpp>
 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:
 </code> </code>
 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 :
-<code c++>+<code cpp>
 if (size != sizeof(X)) return ::new char[size]; if (size != sizeof(X)) return ::new char[size];
 </code> </code>
Line 106: Line 106:
 ==== Item 9 : Avoid hiding the global new ==== ==== Item 9 : Avoid hiding the global new ====
 If you add parameters to your ''new'' redefinition, it blocks access to the usual form of new, so rewrite also the classical form : If you add parameters to your ''new'' redefinition, it blocks access to the usual form of new, so rewrite also the classical form :
-<code c++>+<code cpp>
 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 ''new'' to allocate small objects in a large memory zone (in order to speed up allocations and save memory), but you have also to write ''delete'' ! You can rewrite ''new'' to allocate small objects in a large memory zone (in order to speed up allocations and save memory), but you have also to write ''delete'' !
-<code c++>+<code cpp>
 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 :
-<code c++>+<code cpp>
 C& C::operator=(const C&); C& C::operator=(const C&);
 </code> </code>
Line 269: Line 269:
 ''static_cast<type>(expression)'' : normal cast, for example ''double'' to ''int''.\\ ''static_cast<type>(expression)'' : normal cast, for example ''double'' to ''int''.\\
 ''const_cast'' to cast away the constness or volatileness of an expression :  ''const_cast'' to cast away the constness or volatileness of an expression : 
-<code c++>+<code cpp>
 void update(X *px); void update(X *px);
 const X x; const X x;
Line 276: Line 276:
 ''dynamic_cast'' to perform safe casts down or across an inheritance hierarchy (base to derived objects), and it returns NULL if it fails (or throw an exception when casting references).\\ ''dynamic_cast'' to perform safe casts down or across an inheritance hierarchy (base to derived objects), and it returns NULL if it fails (or throw an exception when casting references).\\
 ''reinterpret_cast'' for implementation-defined casts (rarely portable), for example casting between function pointer types : ''reinterpret_cast'' for implementation-defined casts (rarely portable), for example casting between function pointer types :
-<code c++>+<code cpp>
 typedef void (*FuncPtr)(); // a FuncPtr is a pointer to "void foo()" typedef void (*FuncPtr)(); // a FuncPtr is a pointer to "void foo()"
 FuncPtr funcPtrArray[10]; FuncPtr funcPtrArray[10];
programming/improve-your-cpp.txt ยท Last modified: 2013/09/19 16:40 (external edit)
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0