Download pdf - How to master C++

Transcript
Page 1: How to master C++

How to master C++

Florian Richoux

March 13, 2014

Florian Richoux How to master C++ 1 / 57

Page 2: How to master C++

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 textualcontent, 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, providedyou give appropriate credit to the author, use the material fornon-commercial purposes and distribute your contributions under thesame license as the original. For more information about this license, seehttp://creativecommons.org/licenses/by-nc-sa/4.0/.

Florian Richoux How to master C++ 2 / 57

Page 3: How to master C++

Why C++?

CC BY 2.0

http://www.lextrait.com/vincent/implementations.html

Florian Richoux How to master C++ 3 / 57

Page 4: How to master C++

Why C++?

CC BY 2.0

http://www.lextrait.com/vincent/implementations.html

Florian Richoux How to master C++ 3 / 57

Page 5: How to master C++

Why C++?

CC BY 2.0

http://www.lextrait.com/vincent/implementations.html

Florian Richoux How to master C++ 3 / 57

Page 6: How to master C++

Outline

I Quick recalls about virtualI Object copyI Memory managementI Extra

Randall Munroe, CC BY-NC 2.0

http://xkcd.com/138/

Florian Richoux How to master C++ 4 / 57

Page 7: How to master C++

Some (virtual) recalls

Florian Richoux How to master C++ 5 / 57

Page 8: How to master C++

Some quick recalls

#i n c l u d e <ios t r eam>us ing namespace s t d ;

s t r u c t A {vo id f ( ) { cout << "Class A" << end l ; }

} ;

s t r u c t B: A {vo id f ( ) { cout << "Class B" << end l ; }

} ;

i n t main ( ) {A ∗a = new B;a−>f ( ) ;de le te a ; // ?

}

Output

Class A

Florian Richoux How to master C++ 6 / 57

Page 9: How to master C++

Some quick recalls

#i n c l u d e <ios t r eam>us ing namespace s t d ;

s t r u c t A {vo id f ( ) { cout << "Class A" << end l ; }

} ;

s t r u c t B: A {vo id f ( ) { cout << "Class B" << end l ; }

} ;

i n t main ( ) {A ∗a = new B;a−>f ( ) ;de le te a ; // ?

}

Output

Class A

Florian Richoux How to master C++ 6 / 57

Page 10: How to master C++

Some quick recalls

#i n c l u d e <ios t r eam>us ing namespace s t d ;

s t r u c t A {v i r t u a l vo id f ( ) { cout << "Class A" << end l ; }

} ;

s t r u c t B: A {vo id f ( ) { cout << "Class B" << end l ; }

} ;

i n t main ( ) {A ∗a = new B;a−>f ( ) ;de le te a ; // ?

}

Output

Class B

Florian Richoux How to master C++ 7 / 57

Page 11: How to master C++

Some quick recalls

#i n c l u d e <ios t r eam>us ing namespace s t d ;

s t r u c t A {v i r t u a l vo id f ( ) { cout << "Class A" << end l ; }

} ;

s t r u c t B: A {vo id f ( ) { cout << "Class B" << end l ; }

} ;

i n t main ( ) {A ∗a = new B;a−>f ( ) ;de le te a ; // ?

}

Output

Class B

Florian Richoux How to master C++ 7 / 57

Page 12: How to master C++

Some quick recalls

c l a s s Base{

. . .} ;

c l a s s Der i v ed : pub l i c Base{

~Der i v ed ( ){

// Do some impor tan t c l e anup}

}

Base ∗b = new Der i v ed ( ) ;// use bde le te b ; // Here ' s the problem : ( 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

Page 13: How to master C++

Some quick recalls

c l a s s Base{

pub l i c : v i r t u a l ~Base ( ) { }} ;

c l a s s Der i v ed : pub l i c Base{

~Der i v ed ( ){

// Do some impor tan t c l e anup}

}

Base ∗b = new Der i v ed ( ) ;// use bde le te b ; // c a l l ~De r i v ed ( )

http:

//stackoverflow.com/questions/461203/when-to-use-virtual-destructors

Florian Richoux How to master C++ 9 / 57

Page 14: How to master C++

Some quick recalls

s t r u c t A { v i r t u a l ~A( ) { } } ;s t r u c t B : A { } ;

s t r u c t C { } ;s t r u c t D : C { } ;

i n t main ( ) {B b ;A∗ ap = &b ;A& ar = b ;cout << "ap: " << type id (∗ ap ) . name ( ) << end l ;cout << "ar: " << type id ( a r ) . name ( ) << end l ;

D d ;C∗ cp = &d ;C& c r = d ;cout << "cp: " << type id (∗ cp ) . name ( ) << end l ;cout << "cr: " << type id ( c r ) . name ( ) << end l ;

}

Output

ap: Bar: Bcp: Ccr: 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

Page 15: How to master C++

Some quick recalls

s t r u c t A { v i r t u a l ~A( ) { } } ;s t r u c t B : A { } ;

s t r u c t C { } ;s t r u c t D : C { } ;

i n t main ( ) {B b ;A∗ ap = &b ;A& ar = b ;cout << "ap: " << type id (∗ ap ) . name ( ) << end l ;cout << "ar: " << type id ( a r ) . name ( ) << end l ;

D d ;C∗ cp = &d ;C& c r = d ;cout << "cp: " << type id (∗ cp ) . name ( ) << end l ;cout << "cr: " << type id ( c r ) . name ( ) << end l ;

}

Output

ap: Bar: Bcp: Ccr: 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

Page 16: How to master C++

Some quick recalls

s t r u c t A { v i r t u a l ~A( ) { } } ;s t r u c t B : A { } ;

s t r u c t C { } ;s t r u c t D : C { } ;

i n t main ( ) {B b ;A∗ ap = &b ;A& ar = b ;cout << "ap: " << type id (∗ ap ) . name ( ) << end l ;cout << "ar: " << type id ( a r ) . name ( ) << end l ;

D d ;C∗ cp = &d ;C& c r = d ;cout << "cp: " << type id (∗ cp ) . name ( ) << end l ;cout << "cr: " << type id ( c r ) . name ( ) << end l ;

}

Output

ap: Bar: Bcp: Ccr: 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

Page 17: How to master C++

Object copy

Florian Richoux How to master C++ 11 / 57

Page 18: How to master C++

Copy constructor and copy assignment operator

c l a s s Person{

s td : : s t r i n g name_ ;i n t age_ ;

pub l i c :Person ( s td : : s t r i n g name , i n t age )

: name_(name ) , age_( age ) { }} ;

i n t main ( ){

Person a ( "Bjarne Stroustrup" , 6 3 ) ;Person b ( a ) ; // What happens he r e ?b = a ; // And he r e ?

}

Florian Richoux How to master C++ 12 / 57

Page 19: How to master C++

Copy constructor and copy assignment operator

// 1 . copy c o n s t r u c t o rPerson ( const Person& tha t )

: name_( tha t . name_) , age_( tha t . age_) { }

// 2 . copy as s i gnment op e r a t o rPerson& operator=(const Person& tha t ){

name_ = tha t . name_ ;age_ = tha t . age_ ;re tu rn ∗ t h i s ;

}

Signature

Classname ( const Classname& ) // copy c t o rClassname& operator=( const Classname& ) // as s i gnment op

Florian Richoux How to master C++ 13 / 57

Page 20: How to master C++

Copy constructor and copy assignment operator

i n t main ( ){

Person a ( "Bjarne Stroustrup" , 6 3 ) ;Person b ( a ) ; // C a l l the copy c t o rb = a ; // C a l l the copy as s i gnment op e r a t o rPerson c = a ; // ?

}

More about initializations

http://herbsutter.com/2013/05/09/gotw-1-solution/

Florian Richoux How to master C++ 14 / 57

Page 21: How to master C++

Copy constructor and copy assignment operator

i n t main ( ){

Person a ( "Bjarne Stroustrup" , 6 3 ) ;Person b ( a ) ; // C a l l the copy c t o rb = a ; // C a l l the copy as s i gnment op e r a t o rPerson c = a ; // C a l l the copy c t o r

// ( a lmost e q u i v a l e n t to Person c ( a ) )}

More about initializations

http://herbsutter.com/2013/05/09/gotw-1-solution/

Florian Richoux How to master C++ 15 / 57

Page 22: How to master C++

In which situations is the C++ copy constructor called?

MyClass a ;MyClass b ( a ) ; // copy c o n s t r u c t o r

// //////////////

vo id f oo ( MyClass x ) ;foo ( a ) ; // copy c o n s t r u c t o r

// ( but can be moved i n C++11)// A s imp l e t h i n g to avo i d t h i s ?

// //////////////

MyClass foo ( ){

MyClass temp ;. . .re tu rn temp ; // copy c o n s t r u c t o r

// ( but 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

Page 23: How to master C++

In which situations is the C++ copy constructor called?

MyClass a ; // c o n s t r u c t o rMyClass b ; // c o n s t r u c t o ra = b ; // copy as s i gnment opb = MyClass ( a ) ; // copy c t o r + copy as s i gnment op

MyClass ∗a = new MyClass ( ) ; // c o n s t r u c t o rMyClass ∗b ; // no th ing i s c a l l e db = a ; // s t i l l no th i ng i s c a l l e db = new MyClass (∗ a ) ; // copy 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

Page 24: How to master C++

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 manipulatingmemory, pointers)!

Florian Richoux How to master C++ 18 / 57

Page 25: How to master C++

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 manipulatingmemory, pointers)!

Florian Richoux How to master C++ 18 / 57

Page 26: How to master C++

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 manipulatingmemory, pointers)!

Florian Richoux How to master C++ 18 / 57

Page 27: How to master C++

Why writing a copy ctor and a copy assignment operator?

c l a s s A {pub l i c :A( ) { i = new i n t ; }i n t ∗ i ;

} ;

A a ;A b = a ;// same s t o r y w i th j u s t b = as t d : : cout << a . i << s td : : e nd l << b . i << s td : : e nd l ;

Output

0x3A28213A0x3A28213A

Florian Richoux How to master C++ 19 / 57

Page 28: How to master C++

Why writing a copy ctor and a copy assignment operator?

c l a s s A {pub l i c :A( ) { i = new i n t ; }i n t ∗ i ;

} ;

A a ;A b = a ;// same s t o r y w i th j u s t b = as t d : : cout << a . i << s td : : e nd l << b . i << s td : : e nd l ;

Output

0x3A28213A0x3A28213A

Florian Richoux How to master C++ 19 / 57

Page 29: How to master C++

Why writing a copy ctor and a copy assignment operator?

c l a s s A {pub l i c :A( ) { i = new i n t ; } // c t o r

A( const A& othe r ) { // copy c t o ri = 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 th j u s t b = as t d : : cout << a . i << s td : : e nd l << b . i << s td : : e nd l ;

Output

0x3A28213A0x6339392C

Florian Richoux How to master C++ 20 / 57

Page 30: How to master C++

Why writing a copy ctor and a copy assignment operator?

c l a s s A {pub l i c :A( ) { i = new i n t ; } // c t o r

A( const A& othe r ) { // copy c t o ri = 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 th j u s t b = as t d : : cout << a . i << s td : : e nd l << b . i << s td : : e nd l ;

Output

0x3A28213A0x6339392C

Florian Richoux How to master C++ 20 / 57

Page 31: How to master C++

The copy-and-swap idiom

I explained you:

I What copy ctor and copy assignment operator are.

I When they are called.

I 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

Page 32: How to master C++

The copy-and-swap idiom

c l a s s MyClass {pub l i c :

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 th e r ) // copy 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 td : : copy ( o t h e r . a r r ay , o t h e r . a r r a y + s i z e , a r r a y ) ; }

p r i v a t e :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

Page 33: How to master C++

The copy-and-swap idiom

c l a s s MyClass {. . .pub l i c :

MyClass& operator=(const MyClass& o th e r ) // copy asgmt op{

i f ( t h i s != &othe r ){

// put i n the new data . . .s t d : : s i z e_t newSize = o the r . s i z e ;i n t ∗newArray = newSize ? new i n t [ newSize ] : n u l l p t r ;s t d : : copy ( o th e r . a r r ay , o t h e r . a r r a y + s i z e , newArray ) ;

// . . . and get r i d o f the o l d datade le te [ ] a r r a y ;s i z e = newSize ;a r r a y = newArray ;

}

re tu rn ∗ t h i s ;}

} ;

Florian Richoux How to master C++ 23 / 57

Page 34: How to master C++

The copy-and-swap idiom

c l a s s MyClass {. . .pub l i c :

MyClass& operator=(const MyClass& o th e r ) // copy asgmt op{

i f ( t h i s != &othe r ) // o f t e n u s e l e s s{

// put i n the new data . . .s t d : : s i z e_t newSize = o the r . s i z e ;i n t ∗newArray = newSize ? new i n t [ newSize ] : n u l l p t r ;s t d : : copy ( o th e r . a r r ay , o t h e r . a r r a y + s i z e , newArray ) ;// ( t h e s e 3 l i n e s a r e code d u p l i c a t i o n )

// . . . and get r i d o f the o l d datade le te [ ] a r r a y ;s i z e = newSize ;a r r a y = newArray ;

}

re tu rn ∗ t h i s ;}

} ;

Florian Richoux How to master C++ 24 / 57

Page 35: How to master C++

The copy-and-swap idiom

c l a s s MyClass {. . .pub l i c :

vo id swap ( MyClass& o th e r ){

s td : : swap ( th i s−>s i z e , o t h e r . s i z e ) ;s t d : : swap ( th i s−>ar ray , 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 th e r ) ;re tu rn ∗ t h i s ;

}} ;

http://stackoverflow.com/questions/3279543/

what-is-the-copy-and-swap-idiom/

Florian Richoux How to master C++ 25 / 57

Page 36: How to master C++

Memory management

Florian Richoux How to master C++ 26 / 57

Page 37: How to master C++

The Rule of Three

Rule of 3

If your class needs any of

I a destructor,

I or a copy constructor,

I or a copy assignment operator.

de�ned explicitly, then it is likely to need all three of them.

Put in other words

If your class manages resources, you need to explicitly de�ne:

I a destructor,

I a copy constructor,

I and a copy assignment operator.

Florian Richoux How to master C++ 27 / 57

Page 38: How to master C++

The Rule of Three

These three are linked

What do a copy assignment operator?

I It copies a new state (copy ctor),

I 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

Page 39: How to master C++

The Rule of Three

The rule �A delete for each new� is not su�cient!

c l a s s A{

pub l i c :A( i n t i ) : array_ ( i ? new i n t [ i ] : n u l l p t r ) { }~A( ) { de le te [ ] array_ ; }

p r i v a t e :i n t ∗ array_ ;

} ;

A ∗a1 = new A( 4 2 ) ;A ∗a2 = new A( 2 4 ) ;. . .(∗ a1 ) = (∗ a2 ) ;. . .de le te a1 ;de le te a2 ;

Florian Richoux How to master C++ 29 / 57

Page 40: How to master C++

The Rule of Three

The rule �A delete for each new� is not su�cient!

c l a s s A{

pub l i c :A( i n t i ) : array_ ( i ? new i n t [ i ] : n u l l p t r ) { }~A( ) { de le te [ ] array_ ; }

p r i v a t e :i n t ∗ array_ ;

} ;

A ∗a1 = new A( 4 2 ) ;A ∗a2 = new A( 2 4 ) ;. . .(∗ a1 ) = (∗ a2 ) ; // Memory l e a k !. . . // We have l o s t o r i g i n a l a1 ' s array_de le te a1 ;de le te a2 ; // Unde f ined b eha v i o r !

Florian Richoux How to master C++ 30 / 57

Page 41: How to master C++

C++11 and move semantics

c l a s s Vector {i n t ∗ s torage_ ;s i z e_t s i ze_ ;

pub l i c :

// c t o rVector ( s i z e_t numElements )

: s torage_ (new i n t [ numElements ] ) ,s i ze_ ( numElements )

{ }

// d to r~Vector ( ) { de le te [ ] s torage_ ; }

} ;

http://kholdstare.github.io/technical/2013/11/23/

moves-demystified.html

Florian Richoux How to master C++ 31 / 57

Page 42: How to master C++

C++11 and move semantics

http://kholdstare.github.io/technical/2013/11/23/

moves-demystified.html

Florian Richoux How to master C++ 32 / 57

Page 43: How to master C++

C++11 and move semantics

Vector c = a + b ;

??? operator+ ( Vector const & a , Vecto r const & b ) ;

I Return by value seems bad.

I Return a pointer is bad too: you must make disallocationsomewhere, and can't chained + operations (like a+b+c).

I 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

Page 44: How to master C++

C++11 and move semantics

Vector c = a + b ;

??? operator+ ( Vector const & a , Vecto r const & b ) ;

I Return by value seems bad.

I Return a pointer is bad too: you must make disallocationsomewhere, and can't chained + operations (like a+b+c).

I 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

Page 45: How to master C++

C++11 and move semantics

Vector c = a + b ;

??? operator+ ( Vector const & a , Vecto r const & b ) ;

I Return by value seems bad.

I Return a pointer is bad too: you must make disallocationsomewhere, and can't chained + operations (like a+b+c).

I 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

Page 46: How to master C++

C++11 and move semantics

Vector c = a + b ;

??? operator+ ( Vector const & a , Vecto r const & b ) ;

I Return by value seems bad.

I Return a pointer is bad too: you must make disallocationsomewhere, and can't chained + operations (like a+b+c).

I 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

Page 47: How to master C++

C++11 and move semantics

You need to

move!

Florian Richoux How to master C++ 34 / 57

Page 48: How to master C++

C++11 and move semantics

Is returning by value really bad?

Vector operator+ ( Vector const& a , Vecto r const& b){

// c r e a t e r e s u l t o f same s i z ea s s e r t ( a . s i z e ( ) == b . s i z e ( ) ) ;Vecto r r e s u l t ( a . s i z e ( ) ) ;

// compute a d d i t i o ns t d : : t r an s f o rm (

a . beg i n ( ) , a . end ( ) , // i npu t 1b . beg in ( ) , // i npu t 2r e s u l t . b eg in ( ) , // r e s u l ts t d : : p lu s<i n t >() // b i n a r y o p e r a t i o n

) ;

re tu rn 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

Page 49: How to master C++

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 td : : s t r i n g f i r s t ( "first" ) ;s t d : : s t r i n g second ( "second" ) ;

re tu rn cond ? f i r s t : second ; // r e t u r n under 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

Page 50: How to master C++

C++11 and move semantics

Transferring value into a scope

Ray computeRay ( ){

Vecto r o r i g i n ;Vecto r d i r e c t i o n ;

. . .

re tu rn 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

Page 51: How to master C++

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

Page 52: How to master C++

C++11 and move semantics

Vector : : Vecto r ( Vecto r&& othe r )// s h a l l ow copy

: s torage_ ( o th e r . s torage_ ) ,s i ze_ ( o th e r . s i ze_ )

{// n u l l i f y s ou r c eo th e r . s torage_ = n u l l p t r ;o t h e r . s i ze_ = 0 ;

}

http://kholdstare.github.io/technical/2013/11/23/moves-demystified.html

Florian Richoux How to master C++ 39 / 57

Page 53: How to master C++

C++11 and move semantics

Transferring value into a scope

Ray computeRay ( ){

Vecto r o r i g i n ;Vecto r d i r e c t i o n ;

. . .

re tu rn Ray (s td : : 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

Page 54: How to master C++

The Rule of Four and a Half

When a class manipulates resources

Rule of 4.5

=

Rule of 3

+

de�ne the move ctor(+ de�ne 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

Page 55: How to master C++

RAII and Smart pointers

RAII

Resource Acquisition Is Initialization: release resource automatically.

Some applications

I Files,

I Network sockets,

I Mutex,

I Memory.

Smart pointers std::unique_ptr and std::shared_ptr.

Florian Richoux How to master C++ 42 / 57

Page 56: How to master C++

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

Page 57: How to master C++

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

Page 58: How to master C++

Extra

Florian Richoux How to master C++ 44 / 57

Page 59: How to master C++

I did not talk about

I const (http://duramecho.com/ComputerInformation/WhyHowCppConst.html)

I Exceptions (and C++11 noexecpt)

I operator+= and operator+ (and operator++ and stu�)

I C++ cast

I Functors

I C++11 features like:

auto,

lambda,

decltype

I C++14

I C++17

Florian Richoux How to master C++ 45 / 57

Page 60: How to master C++

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

Page 61: How to master C++

Read!

Books

I E�cient C++ by Scott Meyers (C++11/14 update soon!)

I 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

Page 62: How to master C++

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

Page 63: How to master C++

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

Page 64: How to master C++

Template Metaprogramming

Books

I Modern C++ Design by Andrei Alexandrescu.

I C++ Template Metaprogramming by Dave Abrahams andAleksey Gurtovoy.

I C++ Templates: The Complete Guide by David Vandevoordeand 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

Page 65: How to master C++

SOLID

I Single Responsibility: One reason to exist, one reason to change

I Open Closed Principle: Open for extension, closed for modi�cation

I Liskov Substitution Principle: An object should be semanticallyreplaceable for it's base class/interface

I Interface Segregation Principle: Don't force a client to depend on aninterface it doesn't need to know about

I Dependency Inversion Principle: Depend on abstractions, notconcrete 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

Page 66: How to master C++

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

Page 67: How to master C++

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

Page 68: How to master C++

To make progress

Code!

Teach!

Florian Richoux How to master C++ 53 / 57

Page 69: How to master C++

To make progress

Code!

Teach!

Florian Richoux How to master C++ 53 / 57

Page 70: How to master C++

C++ hiring questions 1/3

I How many ways are there to initialize a primitive data type in C++and what are they?

I Why should you declare a destructor as virtual?I What does it mean that C++ supports overloading?I What are examples of overloading in C++?I What is name mangling in C++ and why is it used?I What is an abstract base class?I What is RTTI?I How can you access a variable that is �hidden� by another variable of

the same name?I What is a namespace and how is it used.I What are the di�erences between a class and a struct in C++, and

how does this compare to C?I What are templates? How are they used?I What is a copy constructor and when is it used, especially in

comparison to the equal operator.I What is the di�erence between a �shallow� and a �deep� copy?I What is the const operator and how is it used?

Florian Richoux How to master C++ 54 / 57

Page 71: How to master C++

C++ hiring questions 2/3

I What are the di�erences between passing by reference, passing byvalue, and passing by pointer in C++?

I When is it and when is it not a good idea to return a value byreference in C++?

I What is the di�erence between a variable created on the stack andone created on the heap?

I How do you free memory allocated dynamically for an array? Whatare the implications of just using delete?

I What is multiple inheritance? When should it be used?

I What is a pure virtual function?

I What does the keyword mutable do?

I What does the keyword volatile do?

I What is the STL?

I What is a Vector?

I What is contained in the <algorithms> header?

Florian Richoux How to master C++ 55 / 57

Page 72: How to master C++

C++ hiring questions 3/3

I What is the di�erence between #include <iostream.h> and#include <iostream>?

I What's the di�erence between �++i� and �i++�?

I What is short circuit evaluation? How can it be used? Why can isbe dangerous?

I What is the `,' operator?

I What is the only ternary operator? How is it used?

I What is the use of a const member function and how can it be used?

I How is try/catch used in C++?

I Why should you never throw an exception in a destructor?

I What is the explicit keyword?

I What is the proper way to perform a cast in C++?

I What does inline do?

Florian Richoux How to master C++ 56 / 57

Page 73: How to master C++

Thanks!

Florian Richoux How to master C++ 57 / 57