Your SlideShare is downloading. ×
How to master C++
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×

Introducing the official SlideShare app

Stunning, full-screen experience for iPhone and Android

Text the download link to your phone

Standard text messaging rates apply

How to master C++

482
views

Published on

These are the slides of my talk organized at the Université de Nantes, on March 13, 2014, by the student association in computer science ASCII. …

These are the slides of my talk organized at the Université de Nantes, on March 13, 2014, by the student association in computer science ASCII.

The target audience is people with some C++ knowledges interested by learning good practices and common
pitfalls.

Published in: Technology, Self Improvement

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
482
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
59
Comments
0
Likes
2
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. How to master C++ Florian Richoux March 13, 2014 Florian Richoux How to master C++ 1/57
  • 2. Licence CC BY-NC-SA 4.0 Attribution-NonCommercial-ShareAlike 4.0 International This talk is licensed CC BY-NC-SA 4.0. This license covers the general organization of the material, the textual content, the gures, etc. except where indicated. All Calvin and Hobbes images are c Bill Watterson. This license means that you can share and adapt this course, provided you give appropriate credit to the author, use the material for non-commercial purposes and distribute your contributions under the same license as the original. For more information about this license, see http://creativecommons.org/licenses/by-nc-sa/4.0/. Florian Richoux How to master C++ 2/57
  • 3. Why C++? Florian Richoux How to master C++ 3/57
  • 4. Why C++? CC BY 2.0 Florian Richoux How to master C++ 3/57
  • 5. Why C++? CC BY 2.0 http://www.lextrait.com/vincent/implementations.html Florian Richoux How to master C++ 3/57
  • 6. Outline Quick recalls about virtual Object copy Memory management Extra Randall Munroe, CC BY-NC 2.0 http://xkcd.com/138/ Florian Richoux How to master C++ 4/57
  • 7. Some (virtual) recalls Florian Richoux How to master C++ 5/57
  • 8. Some quick recalls #i n c l u d e <i o s t r e a m > using namespace s t d ; struct A { void f ( ) { c o u t << "Class A" << e n d l ; } } ; struct B : A { void f ( ) { c o u t << "Class B" << e n d l ; } } ; i n t main ( ) { A ∗a = new B ; a−>f ( ) ; delete a ; // ? } Florian Richoux How to master C++ 6/57
  • 9. Some quick recalls #i n c l u d e <i o s t r e a m > using namespace s t d ; struct A { void f ( ) { c o u t << "Class A" << e n d l ; } } ; struct B : A { void f ( ) { c o u t << "Class B" << e n d l ; } } ; i n t main ( ) { A ∗a = new B ; a−>f ( ) ; delete a ; // ? } Output Class A Florian Richoux How to master C++ 6/57
  • 10. Some quick recalls #i n c l u d e <i o s t r e a m > using namespace s t d ; struct A { v i r t u a l void f ( ) { c o u t << "Class A" << e n d l ; } } ; struct B : A { void f ( ) { c o u t << "Class B" << e n d l ; } } ; i n t main ( ) { A ∗a = new B ; a−>f ( ) ; delete a ; // ? } Florian Richoux How to master C++ 7/57
  • 11. Some quick recalls #i n c l u d e <i o s t r e a m > using namespace s t d ; struct A { v i r t u a l void f ( ) { c o u t << "Class A" << e n d l ; } } ; struct B : A { void f ( ) { c o u t << "Class B" << e n d l ; } } ; i n t main ( ) { A ∗a = new B ; a−>f ( ) ; delete a ; // ? } Output Class B Florian Richoux How to master C++ 7/57
  • 12. Some quick recalls c l a s s Base { . . . } ; c l a s s D e r i v e d : public Base { ~ D e r i v e d ( ) { // Do some i m p o r t a n t c l e a n u p } } Base ∗b = new D e r i v e d ( ) ; // u s e b delete b ; // Here ' s t h e p r o b l e m : ( u s u a l l y ) c a l l ~Base ( ) http: //stackoverflow.com/questions/461203/when-to-use-virtual-destructors Florian Richoux How to master C++ 8/57
  • 13. Some quick recalls c l a s s Base { public : v i r t u a l ~Base ( ) { } } ; c l a s s D e r i v e d : public Base { ~ D e r i v e d ( ) { // Do some i m p o r t a n t c l e a n u p } } Base ∗b = new D e r i v e d ( ) ; // u s e b delete b ; // c a l l ~ D e r i v e d ( ) http: //stackoverflow.com/questions/461203/when-to-use-virtual-destructors Florian Richoux How to master C++ 9/57
  • 14. Some quick recalls struct A { v i r t u a l ~A ( ) { } } ; struct B : A { } ; struct C { } ; struct D : C { } ; i n t main ( ) { B b ; A∗ ap = &b ; A& a r = b ; c o u t << "ap: " << typeid ( ∗ ap ) . name ( ) << e n d l ; c o u t << "ar: " << typeid ( a r ) . name ( ) << e n d l ; D d ; C∗ cp = &d ; C& c r = d ; c o u t << "cp: " << typeid ( ∗ cp ) . name ( ) << e n d l ; c o u t << "cr: " << typeid ( c r ) . name ( ) << e n d l ; } Florian Richoux How to master C++ 10/57
  • 15. Some quick recalls struct A { v i r t u a l ~A ( ) { } } ; struct B : A { } ; struct C { } ; struct D : C { } ; i n t main ( ) { B b ; A∗ ap = &b ; A& a r = b ; c o u t << "ap: " << typeid ( ∗ ap ) . name ( ) << e n d l ; c o u t << "ar: " << typeid ( a r ) . name ( ) << e n d l ; D d ; C∗ cp = &d ; C& c r = d ; c o u t << "cp: " << typeid ( ∗ cp ) . name ( ) << e n d l ; c o u t << "cr: " << typeid ( c r ) . name ( ) << e n d l ; } Output ap: B ar: B cp: C cr: C Florian Richoux How to master C++ 10/57
  • 16. Some quick recalls struct A { v i r t u a l ~A ( ) { } } ; struct B : A { } ; struct C { } ; struct D : C { } ; i n t main ( ) { B b ; A∗ ap = &b ; A& a r = b ; c o u t << "ap: " << typeid ( ∗ ap ) . name ( ) << e n d l ; c o u t << "ar: " << typeid ( a r ) . name ( ) << e n d l ; D d ; C∗ cp = &d ; C& c r = d ; c o u t << "cp: " << typeid ( ∗ cp ) . name ( ) << e n d l ; c o u t << "cr: " << typeid ( c r ) . name ( ) << e n d l ; } Output ap: B ar: B cp: C cr: C http://publib.boulder.ibm.com/infocenter/comphelp/v8v101/index.jsp? topic=%2Fcom.ibm.xlcpp8a.doc%2Flanguage%2Fref%2Fthe_typeid_operator.htm Florian Richoux How to master C++ 10/57
  • 17. Object copy Florian Richoux How to master C++ 11/57
  • 18. Copy constructor and copy assignment operator c l a s s P e r s o n { s t d : : s t r i n g name_ ; i n t age_ ; public : P e r s o n ( s t d : : s t r i n g name , i n t age ) : name_ ( name ) , age_ ( age ) { } } ; i n t main ( ) { P e r s o n a ( "Bjarne Stroustrup" , 6 3 ) ; P e r s o n b ( a ) ; // What h a p p e n s h e r e ? b = a ; // And h e r e ? } Florian Richoux How to master C++ 12/57
  • 19. Copy constructor and copy assignment operator // 1 . c o py c o n s t r u c t o r P e r s o n ( const P e r s o n& t h a t ) : name_ ( t h a t . name_ ) , age_ ( t h a t . age_ ) { } // 2 . c o py a s s i g n m e n t o p e r a t o r P e r s o n& operator=( const P e r s o n& t h a t ) { name_ = t h a t . name_ ; age_ = t h a t . age_ ; return ∗ t h i s ; } Signature C l a s s n a m e ( const C l a s s n a m e& ) // c o py c t o r C l a s s n a m e& operator=( const C l a s s n a m e& ) // a s s i g n m e n t op Florian Richoux How to master C++ 13/57
  • 20. Copy constructor and copy assignment operator i n t main ( ) { P e r s o n a ( "Bjarne Stroustrup" , 6 3 ) ; P e r s o n b ( a ) ; // C a l l t h e co p y c t o r b = a ; // C a l l t h e co p y a s s i g n m e n t o p e r a t o r P e r s o n c = a ; // ? } Florian Richoux How to master C++ 14/57
  • 21. Copy constructor and copy assignment operator i n t main ( ) { P e r s o n a ( "Bjarne Stroustrup" , 6 3 ) ; P e r s o n b ( a ) ; // C a l l t h e co p y c t o r b = a ; // C a l l t h e co p y a s s i g n m e n t o p e r a t o r P e r s o n c = a ; // C a l l t h e co p y c t o r // ( a l m o s t e q u i v a l e n t t o P e r s o n c ( a ) ) } More about initializations http://herbsutter.com/2013/05/09/gotw-1-solution/ Florian Richoux How to master C++ 15/57
  • 22. In which situations is the C++ copy constructor called? MyClass a ; MyClass b ( a ) ; // co py c o n s t r u c t o r // ////////////// void f o o ( MyClass x ) ; f o o ( a ) ; // co py c o n s t r u c t o r // ( b u t can be moved i n C++11) // A s i m p l e t h i n g t o a v o i d t h i s ? // ////////////// MyClass f o o ( ) { MyClass temp ; . . . return temp ; // co py c o n s t r u c t o r // ( b u t u s u a l l y RVO a p p l i e s ) } http://stackoverflow.com/questions/21206359/ in-which-situations-is-the-c-copy-constructor-called Florian Richoux How to master C++ 16/57
  • 23. In which situations is the C++ copy constructor called? MyClass a ; // c o n s t r u c t o r MyClass b ; // c o n s t r u c t o r a = b ; // co py a s s i g n m e n t op b = MyClass ( a ) ; // co py c t o r + co py a s s i g n m e n t op MyClass ∗a = new MyClass ( ) ; // c o n s t r u c t o r MyClass ∗b ; // n o t h i n g i s c a l l e d b = a ; // s t i l l n o t h i n g i s c a l l e d b = new MyClass ( ∗ a ) ; // co py c o n s t r u c t o r http://stackoverflow.com/questions/21206359/ in-which-situations-is-the-c-copy-constructor-called Florian Richoux How to master C++ 17/57
  • 24. Why writing a copy ctor and a copy assignment operator? Question Why do we need to (sometimes) write them? Florian Richoux How to master C++ 18/57
  • 25. Why writing a copy ctor and a copy assignment operator? Question Why do we need to (sometimes) write them? Reformulated question When do we need to write them? Florian Richoux How to master C++ 18/57
  • 26. Why writing a copy ctor and a copy assignment operator? Question Why do we need to (sometimes) write them? Reformulated question When do we need to write them? Answer Each time you have a class managing resources (like manipulating memory, pointers)! Florian Richoux How to master C++ 18/57
  • 27. Why writing a copy ctor and a copy assignment operator? c l a s s A { public : A ( ) { i = new i n t ; } i n t ∗ i ; } ; A a ; A b = a ; // same s t o r y w i t h j u s t b = a s t d : : c o u t << a . i << s t d : : e n d l << b . i << s t d : : e n d l ; Florian Richoux How to master C++ 19/57
  • 28. Why writing a copy ctor and a copy assignment operator? c l a s s A { public : A ( ) { i = new i n t ; } i n t ∗ i ; } ; A a ; A b = a ; // same s t o r y w i t h j u s t b = a s t d : : c o u t << a . i << s t d : : e n d l << b . i << s t d : : e n d l ; Output 0x3A28213A 0x3A28213A Florian Richoux How to master C++ 19/57
  • 29. Why writing a copy ctor and a copy assignment operator? c l a s s A { public : A ( ) { i = new i n t ; } // c t o r A( const A& o t h e r ) { // co py c t o r i = new i n t ; ∗ i = ∗( o t h e r . i ) ; } i n t ∗ i ; } ; A a ; A b = a ; // same s t o r y w i t h j u s t b = a s t d : : c o u t << a . i << s t d : : e n d l << b . i << s t d : : e n d l ; Florian Richoux How to master C++ 20/57
  • 30. Why writing a copy ctor and a copy assignment operator? c l a s s A { public : A ( ) { i = new i n t ; } // c t o r A( const A& o t h e r ) { // co py c t o r i = new i n t ; ∗ i = ∗( o t h e r . i ) ; } i n t ∗ i ; } ; A a ; A b = a ; // same s t o r y w i t h j u s t b = a s t d : : c o u t << a . i << s t d : : e n d l << b . i << s t d : : e n d l ; Output 0x3A28213A 0x6339392C Florian Richoux How to master C++ 20/57
  • 31. The copy-and-swap idiom I explained you: What copy ctor and copy assignment operator are. When they are called. Why it is important to (sometimes) write them. But I did not explain yet how to implement them properly. Good implementation Apply the copy-and-swap idiom. Florian Richoux How to master C++ 21/57
  • 32. The copy-and-swap idiom c l a s s MyClass { public : MyClass ( s t d : : s i z e _ t s i z e = 0 ) // c t o r : s i z e ( s i z e ) , a r r a y ( s i z e ? new i n t [ s i z e ] : n u l l p t r ) {} MyClass ( const MyClass& o t h e r ) // co py c t o r : s i z e ( o t h e r . s i z e ) , a r r a y ( s i z e ? new i n t [ s i z e ] : n u l l p t r ) { s t d : : co py ( o t h e r . a r r a y , o t h e r . a r r a y + s i z e , a r r a y ) ; } private : s t d : : s i z e _ t s i z e ; i n t ∗ a r r a y ; } ; http://stackoverflow.com/questions/3279543/ what-is-the-copy-and-swap-idiom/ Florian Richoux How to master C++ 22/57
  • 33. The copy-and-swap idiom c l a s s MyClass { . . . public : MyClass& operator=( const MyClass& o t h e r ) // co py asgmt op { i f ( t h i s != &o t h e r ) { // p u t i n t h e new d a t a . . . s t d : : s i z e _ t n e w S i z e = o t h e r . s i z e ; i n t ∗ n e w A r r a y = n e w S i z e ? new i n t [ n e w S i z e ] : n u l l p t r ; s t d : : co p y ( o t h e r . a r r a y , o t h e r . a r r a y + s i z e , n e w A r r a y ) ; // . . . and g e t r i d o f t h e o l d d a t a delete [ ] a r r a y ; s i z e = n e w S i z e ; a r r a y = n e w A r r a y ; } return ∗ t h i s ; } } ; Florian Richoux How to master C++ 23/57
  • 34. The copy-and-swap idiom c l a s s MyClass { . . . public : MyClass& operator=( const MyClass& o t h e r ) // co py asgmt op { i f ( t h i s != &o t h e r ) // o f t e n u s e l e s s { // p u t i n t h e new d a t a . . . s t d : : s i z e _ t n e w S i z e = o t h e r . s i z e ; i n t ∗ n e w A r r a y = n e w S i z e ? new i n t [ n e w S i z e ] : n u l l p t r ; s t d : : co p y ( o t h e r . a r r a y , o t h e r . a r r a y + s i z e , n e w A r r a y ) ; // ( t h e s e 3 l i n e s a r e c o d e d u p l i c a t i o n ) // . . . and g e t r i d o f t h e o l d d a t a delete [ ] a r r a y ; s i z e = n e w S i z e ; a r r a y = n e w A r r a y ; } return ∗ t h i s ; } } ; Florian Richoux How to master C++ 24/57
  • 35. The copy-and-swap idiom c l a s s MyClass { . . . public : void swap ( MyClass& o t h e r ) { s t d : : swap ( this −>s i z e , o t h e r . s i z e ) ; s t d : : swap ( this −>a r r a y , o t h e r . a r r a y ) ; } MyClass& operator=( MyClass o t h e r ) // no r e f e r e n c e ! { swap ( o t h e r ) ; return ∗ t h i s ; } } ; http://stackoverflow.com/questions/3279543/ what-is-the-copy-and-swap-idiom/ Florian Richoux How to master C++ 25/57
  • 36. Memory management Florian Richoux How to master C++ 26/57
  • 37. The Rule of Three Rule of 3 If your class needs any of a destructor, or a copy constructor, or a copy assignment operator. dened explicitly, then it is likely to need all three of them. Put in other words If your class manages resources, you need to explicitly dene: a destructor, a copy constructor, and a copy assignment operator. Florian Richoux How to master C++ 27/57
  • 38. The Rule of Three These three are linked What do a copy assignment operator? It copies a new state (copy ctor), and it deletes the old state (destructor). http://stackoverflow.com/questions/4172722/ what-is-the-rule-of-three Florian Richoux How to master C++ 28/57
  • 39. The Rule of Three The rule A delete for each new is not sucient! c l a s s A { public : A( i n t i ) : a r r a y _ ( i ? new i n t [ i ] : n u l l p t r ) { } ~A ( ) { delete [ ] a r r a y _ ; } private : i n t ∗ a r r a y _ ; } ; A ∗a1 = new A ( 4 2 ) ; A ∗a2 = new A ( 2 4 ) ; . . . ( ∗ a1 ) = ( ∗ a2 ) ; . . . delete a1 ; delete a2 ; Florian Richoux How to master C++ 29/57
  • 40. The Rule of Three The rule A delete for each new is not sucient! c l a s s A { public : A( i n t i ) : a r r a y _ ( i ? new i n t [ i ] : n u l l p t r ) { } ~A ( ) { delete [ ] a r r a y _ ; } private : i n t ∗ a r r a y _ ; } ; A ∗a1 = new A ( 4 2 ) ; A ∗a2 = new A ( 2 4 ) ; . . . ( ∗ a1 ) = ( ∗ a2 ) ; // Memory l e a k ! . . . // We h a v e l o s t o r i g i n a l a1 ' s a r r a y _ delete a1 ; delete a2 ; // U n d e f i n e d b e h a v i o r ! Florian Richoux How to master C++ 30/57
  • 41. C++11 and move semantics c l a s s V e c t o r { i n t ∗ s t o r a g e _ ; s i z e _ t s i z e _ ; public : // c t o r V e c t o r ( s i z e _ t numElements ) : s t o r a g e _ ( new i n t [ numElements ] ) , s i z e _ ( numElements ) { } // d t o r ~ V e c t o r ( ) { delete [ ] s t o r a g e _ ; } } ; http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 31/57
  • 42. C++11 and move semantics http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 32/57
  • 43. C++11 and move semantics V e c t o r c = a + b ; ? ? ? operator+ ( V e c t o r const & a , V e c t o r const & b ) ; http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 33/57
  • 44. C++11 and move semantics V e c t o r c = a + b ; ? ? ? operator+ ( V e c t o r const & a , V e c t o r const & b ) ; Return by value seems bad. http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 33/57
  • 45. C++11 and move semantics V e c t o r c = a + b ; ? ? ? operator+ ( V e c t o r const & a , V e c t o r const & b ) ; Return by value seems bad. Return a pointer is bad too: you must make disallocation somewhere, and can't chained + operations (like a+b+c). http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 33/57
  • 46. C++11 and move semantics V e c t o r c = a + b ; ? ? ? operator+ ( V e c t o r const & a , V e c t o r const & b ) ; Return by value seems bad. Return a pointer is bad too: you must make disallocation somewhere, and can't chained + operations (like a+b+c). Return a reference seems not a good idea either. http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 33/57
  • 47. C++11 and move semantics You need to move! Florian Richoux How to master C++ 34/57
  • 48. C++11 and move semantics Is returning by value really bad? V e c t o r operator+ ( V e c t o r const& a , V e c t o r const& b ) { // c r e a t e r e s u l t o f same s i z e a s s e r t ( a . s i z e ( ) == b . s i z e ( ) ) ; V e c t o r r e s u l t ( a . s i z e ( ) ) ; // compute a d d i t i o n s t d : : t r a n s f o r m ( a . b e g i n ( ) , a . end ( ) , // i n p u t 1 b . b e g i n ( ) , // i n p u t 2 r e s u l t . b e g i n ( ) , // r e s u l t s t d : : p l u s <int >() // b i n a r y o p e r a t i o n ) ; return r e s u l t ; // RVO u s u a l l y a p p l i e s } http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 35/57
  • 49. C++11 and move semantics Yes, but... Reason #1 s t d : : s t r i n g f ( bool cond = f a l s e ) { s t d : : s t r i n g f i r s t ( "first" ) ; s t d : : s t r i n g s e c o n d ( "second" ) ; return cond ? f i r s t : s e c o n d ; // r e t u r n u n d e r c o n d i t i o n : // u s u a l l y no RVO } Reason #2 RVO applies when one transfers a value out of a scope. What if we need to transfer into a scope? http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html http://en.wikipedia.org/wiki/Return_value_optimization Florian Richoux How to master C++ 36/57
  • 50. C++11 and move semantics Transferring value into a scope Ray computeRay ( ) { V e c t o r o r i g i n ; V e c t o r d i r e c t i o n ; . . . return Ray ( o r i g i n , // COPY! d i r e c t i o n // COPY! ) ; // c e r t a i n l y RVO } http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 37/57
  • 51. lvalue Vs rvalue lvalue c = a + b; rvalue c = a + b; Must be a temporary, non-named value. http://stackoverflow.com/questions/3601602/ what-are-rvalues-lvalues-xvalues-glvalues-and-prvalues Florian Richoux How to master C++ 38/57
  • 52. C++11 and move semantics V e c t o r : : V e c t o r ( V e c t o r&& o t h e r ) // s h a l l o w co p y : s t o r a g e _ ( o t h e r . s t o r a g e _ ) , s i z e _ ( o t h e r . s i z e _ ) { // n u l l i f y s o u r c e o t h e r . s t o r a g e _ = n u l l p t r ; o t h e r . s i z e _ = 0 ; } http://kholdstare.github.io/technical/2013/11/23/moves-demystified.html Florian Richoux How to master C++ 39/57
  • 53. C++11 and move semantics Transferring value into a scope Ray computeRay ( ) { V e c t o r o r i g i n ; V e c t o r d i r e c t i o n ; . . . return Ray ( s t d : : move ( o r i g i n ) , // moved ! s t d : : move ( d i r e c t i o n ) // moved ! ) ; // c e r t a i n l y RVO } http://kholdstare.github.io/technical/2013/11/23/ moves-demystified.html Florian Richoux How to master C++ 40/57
  • 54. The Rule of Four and a Half When a class manipulates resources Rule of 4.5 = Rule of 3 + dene the move ctor (+ dene a move assignment operator?) http://stackoverflow.com/questions/4782757/ rule-of-three-becomes-rule-of-five-with-c11 http: //stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom Florian Richoux How to master C++ 41/57
  • 55. RAII and Smart pointers RAII Resource Acquisition Is Initialization: release resource automatically. Some applications Files, Network sockets, Mutex, Memory. Smart pointers std::unique_ptr and std::shared_ptr. Florian Richoux How to master C++ 42/57
  • 56. Rule of Zero Rule of 0 Using smart pointers (and RAII principle) to manage resources, no need to explicitly declare dtor, copy ctor, etc. http://flamingdangerzone.com/cxx11/2012/08/15/rule-of-zero.html Florian Richoux How to master C++ 43/57
  • 57. Rule of Zero Rule of 0 Using smart pointers (and RAII principle) to manage resources, no need to explicitly declare dtor, copy ctor, etc. You can rest! http://flamingdangerzone.com/cxx11/2012/08/15/rule-of-zero.html Florian Richoux How to master C++ 43/57
  • 58. Extra Florian Richoux How to master C++ 44/57
  • 59. I did not talk about const (http://duramecho.com/ComputerInformation/WhyHowCppConst.html) Exceptions (and C++11 noexecpt) operator+= and operator+ (and operator++ and stu) C++ cast Functors C++11 features like: auto, lambda, decltype C++14 C++17 Florian Richoux How to master C++ 45/57
  • 60. Safety Asserts Use assert. Unable them with the -DNDEBUG compile option. Valgrind valgrind leak-check=full show-reachable=yes ./your_program Warnings Try to solve them! Florian Richoux How to master C++ 46/57
  • 61. Read! Books Ecient C++ by Scott Meyers (C++11/14 update soon!) Exceptionnal C++ by Herb Sutter (C++11/14 update soon!) Blog Herb Sutter's Guru of the Week http://herbsutter.com/category/c/gotw/ Twitter @isocpp @cppstack Florian Richoux How to master C++ 47/57
  • 62. Use! Boost library http://www.boost.org/ <algorithm> Gotta use 'em all! http://www.cplusplus.com/reference/algorithm/ Florian Richoux How to master C++ 48/57
  • 63. Fonctional C++ John Carmack's blog http://www.altdevblogaday.com/2012/04/26/ functional-programming-in-c/ Modern Functional Programming in C++ http://zao.se/~zao/boostcon/10/2010_presentations/thu/ funccpp.pdf C++17: I See a Monad in Your Future! http://bartoszmilewski.com/2014/02/26/ c17-i-see-a-monad-in-your-future/ Florian Richoux How to master C++ 49/57
  • 64. Template Metaprogramming Books Modern C++ Design by Andrei Alexandrescu. C++ Template Metaprogramming by Dave Abrahams and Aleksey Gurtovoy. C++ Templates: The Complete Guide by David Vandevoorde and Nicolai Josuttis (second edition planned for 2015). A nice intro http://www.codeproject.com/Articles/3743/ A-gentle-introduction-to-Template-Metaprogramming Florian Richoux How to master C++ 50/57
  • 65. SOLID Single Responsibility: One reason to exist, one reason to change Open Closed Principle: Open for extension, closed for modication Liskov Substitution Principle: An object should be semantically replaceable for it's base class/interface Interface Segregation Principle: Don't force a client to depend on an interface it doesn't need to know about Dependency Inversion Principle: Depend on abstractions, not concrete detail or implementations http://stackoverflow.com/questions/1423597/solid-principles http://en.wikipedia.org/wiki/SOLID_%28object-oriented_design%29 Florian Richoux How to master C++ 51/57
  • 66. Repository and comments svn, git, mercurial, ... Ultimate combo GitHub + Travis (http://docs.travis-ci.com/user/getting-started/) Comments Comment your code with doxygen Florian Richoux How to master C++ 52/57
  • 67. Repository and comments svn, git, mercurial, ... Ultimate combo GitHub + Travis (http://docs.travis-ci.com/user/getting-started/) Comments Comment your code with doxygen in English! Florian Richoux How to master C++ 52/57
  • 68. To make progress Code! Florian Richoux How to master C++ 53/57
  • 69. To make progress Code! Teach! Florian Richoux How to master C++ 53/57
  • 70. C++ hiring questions 1/3 How many ways are there to initialize a primitive data type in C++ and what are they? Why should you declare a destructor as virtual? What does it mean that C++ supports overloading? What are examples of overloading in C++? What is name mangling in C++ and why is it used? What is an abstract base class? What is RTTI? How can you access a variable that is hidden by another variable of the same name? What is a namespace and how is it used. What are the dierences between a class and a struct in C++, and how does this compare to C? What are templates? How are they used? What is a copy constructor and when is it used, especially in comparison to the equal operator. What is the dierence between a shallow and a deep copy? What is the const operator and how is it used? Florian Richoux How to master C++ 54/57
  • 71. C++ hiring questions 2/3 What are the dierences between passing by reference, passing by value, and passing by pointer in C++? When is it and when is it not a good idea to return a value by reference in C++? What is the dierence between a variable created on the stack and one created on the heap? How do you free memory allocated dynamically for an array? What are the implications of just using delete? What is multiple inheritance? When should it be used? What is a pure virtual function? What does the keyword mutable do? What does the keyword volatile do? What is the STL? What is a Vector? What is contained in the <algorithms> header? Florian Richoux How to master C++ 55/57
  • 72. C++ hiring questions 3/3 What is the dierence between #include <iostream.h> and #include <iostream>? What's the dierence between ++i and i++? What is short circuit evaluation? How can it be used? Why can is be dangerous? What is the `,' operator? What is the only ternary operator? How is it used? What is the use of a const member function and how can it be used? How is try/catch used in C++? Why should you never throw an exception in a destructor? What is the explicit keyword? What is the proper way to perform a cast in C++? What does inline do? Florian Richoux How to master C++ 56/57
  • 73. Thanks! Florian Richoux How to master C++ 57/57