SlideShare a Scribd company logo
How to master C++
Florian Richoux
March 13, 2014
Florian Richoux How to master C++ 1/57
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
Why C++?
Florian Richoux How to master C++ 3/57
Why C++?
CC BY 2.0
Florian Richoux How to master C++ 3/57
Why C++?
CC BY 2.0
http://www.lextrait.com/vincent/implementations.html
Florian Richoux How to master C++ 3/57
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
Some (virtual) recalls
Florian Richoux How to master C++ 5/57
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
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
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
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
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
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
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
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
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
Object copy
Florian Richoux How to master C++ 11/57
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
Memory management
Florian Richoux How to master C++ 26/57
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
The Rule of Three
These three are linked
What does a copy assignment operator do?
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
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
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
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
C++11 and move semantics
http://kholdstare.github.io/technical/2013/11/23/
moves-demystified.html
Florian Richoux How to master C++ 32/57
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
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
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
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
C++11 and move semantics
You need to
move!
Florian Richoux How to master C++ 34/57
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
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
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
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
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
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
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
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
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
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
Extra
Florian Richoux How to master C++ 44/57
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
Most of C++11/14/17 features
(https://github.com/AnthonyCalandra/modern-cpp-features)
Florian Richoux How to master C++ 45/57
Safety
Asserts
Use assert. Disable them with the -DNDEBUG compile option.
Valgrind
valgrind leak-check=full show-reachable=yes ./your_program
Wonderful GUI: kcachegrind
Warnings
Try to solve them!
Florian Richoux How to master C++ 46/57
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
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
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
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
A nice and modern intro
http://www.oreilly.com/programming/free/
practical-c-plus-plus-metaprogramming.csp (Free e-book!)
Florian Richoux How to master C++ 50/57
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
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
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
To make progress
Code!
Florian Richoux How to master C++ 53/57
To make progress
Code!
Teach!
Florian Richoux How to master C++ 53/57
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
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
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
Thanks!
Florian Richoux How to master C++ 57/57

More Related Content

What's hot

C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
Chris Ohk
 
C mcq practice test 1
C mcq practice test 1C mcq practice test 1
C mcq practice test 1
Aman Kamboj
 
GraphQL Relay Introduction
GraphQL Relay IntroductionGraphQL Relay Introduction
GraphQL Relay Introduction
Chen-Tsu Lin
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
Chris Ohk
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
Chris Ohk
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
勇浩 赖
 
Scope and closures
Scope and closuresScope and closures
Scope and closures
Monu Chaudhary
 

What's hot (11)

C
CC
C
 
week-12x
week-12xweek-12x
week-12x
 
C++ Programming - 11th Study
C++ Programming - 11th StudyC++ Programming - 11th Study
C++ Programming - 11th Study
 
C mcq practice test 1
C mcq practice test 1C mcq practice test 1
C mcq practice test 1
 
GraphQL Relay Introduction
GraphQL Relay IntroductionGraphQL Relay Introduction
GraphQL Relay Introduction
 
Data Structure - 2nd Study
Data Structure - 2nd StudyData Structure - 2nd Study
Data Structure - 2nd Study
 
C programming slide c04
C programming slide c04C programming slide c04
C programming slide c04
 
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13[C++ Korea] Effective Modern C++ Study, Item 11 - 13
[C++ Korea] Effective Modern C++ Study, Item 11 - 13
 
为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?为什么 rust-lang 吸引我?
为什么 rust-lang 吸引我?
 
Vcs15
Vcs15Vcs15
Vcs15
 
Scope and closures
Scope and closuresScope and closures
Scope and closures
 

Similar to How to master C++

C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
Programming Homework Help
 
C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?
Mateusz Pusz
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)Kai-Feng Chou
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
ranaibrahim453
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorJussi Pohjolainen
 
Beyond C++17
Beyond C++17Beyond C++17
Beyond C++17
Mateusz Pusz
 
C++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPAC++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPA
Isabella789
 
(Slightly) Smarter Smart Pointers
(Slightly) Smarter Smart Pointers(Slightly) Smarter Smart Pointers
(Slightly) Smarter Smart Pointers
Carlo Pescio
 
report
reportreport
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdf
choconyeuquy
 
C++ Core Guidelines
C++ Core GuidelinesC++ Core Guidelines
C++ Core Guidelines
Thomas Pollak
 
ESL Anyone?
ESL Anyone? ESL Anyone?
ESL Anyone? DVClub
 
What every C++ programmer should know about modern compilers (w/o comments, A...
What every C++ programmer should know about modern compilers (w/o comments, A...What every C++ programmer should know about modern compilers (w/o comments, A...
What every C++ programmer should know about modern compilers (w/o comments, A...
Sławomir Zborowski
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
Yi-Hsiu Hsu
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++
Alexander Granin
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
Harleen Sodhi
 
Diving into HHVM Extensions (Brno PHP Conference 2015)
Diving into HHVM Extensions (Brno PHP Conference 2015)Diving into HHVM Extensions (Brno PHP Conference 2015)
Diving into HHVM Extensions (Brno PHP Conference 2015)
James Titcumb
 
[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary
[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary	[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary
[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary
EnlightenmentProject
 
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detection
Võ Hòa
 
computer notes - Data Structures - 25
computer notes - Data Structures - 25computer notes - Data Structures - 25
computer notes - Data Structures - 25ecomputernotes
 

Similar to How to master C++ (20)

C Programming Homework Help
C Programming Homework HelpC Programming Homework Help
C Programming Homework Help
 
C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?C++ Concepts and Ranges - How to use them?
C++ Concepts and Ranges - How to use them?
 
OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)OOP in C - Before GObject (Chinese Version)
OOP in C - Before GObject (Chinese Version)
 
Lab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practiceLab Manual IV (1).pdf on C++ Programming practice
Lab Manual IV (1).pdf on C++ Programming practice
 
C++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operatorC++: Constructor, Copy Constructor and Assignment operator
C++: Constructor, Copy Constructor and Assignment operator
 
Beyond C++17
Beyond C++17Beyond C++17
Beyond C++17
 
C++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPAC++ Certified Associate Programmer CPA
C++ Certified Associate Programmer CPA
 
(Slightly) Smarter Smart Pointers
(Slightly) Smarter Smart Pointers(Slightly) Smarter Smart Pointers
(Slightly) Smarter Smart Pointers
 
report
reportreport
report
 
C multiple choice questions and answers pdf
C multiple choice questions and answers pdfC multiple choice questions and answers pdf
C multiple choice questions and answers pdf
 
C++ Core Guidelines
C++ Core GuidelinesC++ Core Guidelines
C++ Core Guidelines
 
ESL Anyone?
ESL Anyone? ESL Anyone?
ESL Anyone?
 
What every C++ programmer should know about modern compilers (w/o comments, A...
What every C++ programmer should know about modern compilers (w/o comments, A...What every C++ programmer should know about modern compilers (w/o comments, A...
What every C++ programmer should know about modern compilers (w/o comments, A...
 
Understand more about C
Understand more about CUnderstand more about C
Understand more about C
 
The present and the future of functional programming in c++
The present and the future of functional programming in c++The present and the future of functional programming in c++
The present and the future of functional programming in c++
 
Wap to implement bitwise operators
Wap to implement bitwise operatorsWap to implement bitwise operators
Wap to implement bitwise operators
 
Diving into HHVM Extensions (Brno PHP Conference 2015)
Diving into HHVM Extensions (Brno PHP Conference 2015)Diving into HHVM Extensions (Brno PHP Conference 2015)
Diving into HHVM Extensions (Brno PHP Conference 2015)
 
[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary
[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary	[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary
[E-Dev-Day 2014][5/16] C++ and JavaScript bindings for EFL and Elementary
 
C++ memory leak detection
C++ memory leak detectionC++ memory leak detection
C++ memory leak detection
 
computer notes - Data Structures - 25
computer notes - Data Structures - 25computer notes - Data Structures - 25
computer notes - Data Structures - 25
 

Recently uploaded

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
g2nightmarescribd
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
OnBoard
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
BookNet Canada
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Product School
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
Product School
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
Dorra BARTAGUIZ
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 

Recently uploaded (20)

State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Generating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using SmithyGenerating a custom Ruby SDK for your web service or Rails API using Smithy
Generating a custom Ruby SDK for your web service or Rails API using Smithy
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
Leading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdfLeading Change strategies and insights for effective change management pdf 1.pdf
Leading Change strategies and insights for effective change management pdf 1.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...Transcript: Selling digital books in 2024: Insights from industry leaders - T...
Transcript: Selling digital books in 2024: Insights from industry leaders - T...
 
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
Unsubscribed: Combat Subscription Fatigue With a Membership Mentality by Head...
 
How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...How world-class product teams are winning in the AI era by CEO and Founder, P...
How world-class product teams are winning in the AI era by CEO and Founder, P...
 
Elevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object CalisthenicsElevating Tactical DDD Patterns Through Object Calisthenics
Elevating Tactical DDD Patterns Through Object Calisthenics
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 

How to master C++

  • 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 does a copy assignment operator do? 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 Most of C++11/14/17 features (https://github.com/AnthonyCalandra/modern-cpp-features) Florian Richoux How to master C++ 45/57
  • 60. Safety Asserts Use assert. Disable them with the -DNDEBUG compile option. Valgrind valgrind leak-check=full show-reachable=yes ./your_program Wonderful GUI: kcachegrind 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 A nice and modern intro http://www.oreilly.com/programming/free/ practical-c-plus-plus-metaprogramming.csp (Free e-book!) 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