11idiom - C idioms Now that you've learned C how do we use...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
C++ idioms Now that you've learned C++, how do we use it well A highly recommended book: Scott Meyers Effective C++: 50 Specific Ways to Improve your Programs and Designs , Second Edition, Addison-Wesley, 1997. (Also More Effective C++: 35 New Ways. ... ) Scott Meyers discusses 50 "items"--good principles, heuristics or idioms, with good explanations 1 Use const and inline instead of #define . #define RATIO 1.653 /*A C macro*/ const float RATIO = 1.653; // Why is a C++ const better? //better compiler error messages, easier symbolic debugging //obeys scope rules #define max(a,b) ( (a) > (b) ? (a) : (b) ) /* A parameterized C macro */ int a=1,b=0; //Let's see what can wrong in a parameterized C macro. .. max(a++,b); //a is incremented twice-- why? (in condition and result) max(a++,b+10) //b is incremented just once max(a,"hello") //compares ints and ptrs (no type checking) inline int max(int a,int b) { return a > b ? a : b; } //both efficient and type safe //Use a template to generate a family of type safe functions: template<class T> inline T& max(T& a, T& b) { return a > b ? a : b; } //then let the compiler instantiate the template with various types: max(a++,b); //compiler generates max with int arguments -- and a is incremented once max(RATIO,b); //compiler generates max with float arguments In item 1, Meyers used to have a forward reference to item 32. Indeed, his book reads like hypertext! But now item 32 has been rewritten (due to changes in C++) and folded into item 1 32 Use enum s for integral class constants Suppose you would like to encapsulate a const within a class class X { static const BUFSIZE=100; //error with older compilers, but now acceptable char buffer[BUFSIZE]; }; //couldn = t initialize static members at the point of its declaration, but you could do this: const double X::BUFSIZE=100; //goes in class implementation file // BTW, why did Meyers want to define this a static const instead of just const ? Another solution is to use an enum, which can be given a value at the point of declaration: enum { BUFSIZE=100 } ; char buffer[BUFSIZE]; enum hack should not be necessary for compilers implemented since 1995 33 Use inlining judiciously --why? excessive inlines can cause code bloat, also hard to debug ( why? ) Meyers recommend you limit inlining to truly trivial functions (set and get functions) Even empty constructors can involve lots of hidden code inserted by compiler (pp. 140-1) the honored 80-20 rule: typical program spends 80% of its time performing 20% of its code --so inline and otherwise tweak that crucial 20%, later on Constructors and Destructors -1-
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
12 Prefer initialization to assignment in constructors class NamedData { string name; void *data; public: NamedData(const string& initName, void *dataPtr); }; Two ways to implement this constructor, one using assignment { name = initName; data=dataPtr; } or using member initialization:
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page1 / 8

11idiom - C idioms Now that you've learned C how do we use...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online