How to master C++

67 %
33 %
Information about How to master C++
Technology

Published on March 14, 2014

Author: FlorianRichoux

Source: slideshare.net

Description

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

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

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-SA 4.0. This license covers the general organization of the material, the textual content, the figures, 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 <iostream > using namespace std ; s t r u c t A { void f () { cout << "Class␣A" << endl ; } }; s t r u c t B: A { void f () { cout << "Class␣B" << endl ; } }; 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 <iostream > using namespace std ; s t r u c t A { void f () { cout << "Class␣A" << endl ; } }; s t r u c t B: A { void f () { cout << "Class␣B" << endl ; } }; 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 <iostream > using namespace std ; s t r u c t A { v i r t u a l void f () { cout << "Class␣A" << endl ; } }; s t r u c t B: A { void f () { cout << "Class␣B" << endl ; } }; 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 <iostream > using namespace std ; s t r u c t A { v i r t u a l void f () { cout << "Class␣A" << endl ; } }; s t r u c t B: A { void f () { cout << "Class␣B" << endl ; } }; 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 Derived : p u b l ic Base { ~Derived () { // Do some important cleanup } } Base ∗b = new Derived ( ) ; // use b delete 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

Some quick recalls c l a s s Base { p u b l i c : v i r t u a l ~Base () { } }; c l a s s Derived : p u b l ic Base { ~Derived () { // Do some important cleanup } } Base ∗b = new Derived ( ) ; // use b delete b ; // c a l l ~Derived () http: //stackoverflow.com/questions/461203/when-to-use-virtual-destructors Florian Richoux How to master C++ 9 / 57

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:␣" << typeid (∗ ap ) . name () << endl ; cout << "ar:␣" << typeid ( ar ) . name () << endl ; D d ; C∗ cp = &d ; C& cr = d ; cout << "cp:␣" << typeid (∗ cp ) . name () << endl ; cout << "cr:␣" << typeid ( cr ) . name () << endl ; } Florian Richoux How to master C++ 10 / 57

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:␣" << typeid (∗ ap ) . name () << endl ; cout << "ar:␣" << typeid ( ar ) . name () << endl ; D d ; C∗ cp = &d ; C& cr = d ; cout << "cp:␣" << typeid (∗ cp ) . name () << endl ; cout << "cr:␣" << typeid ( cr ) . name () << endl ; } Output ap: B ar: B cp: C cr: C Florian Richoux How to master C++ 10 / 57

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:␣" << typeid (∗ ap ) . name () << endl ; cout << "ar:␣" << typeid ( ar ) . name () << endl ; D d ; C∗ cp = &d ; C& cr = d ; cout << "cp:␣" << typeid (∗ cp ) . name () << endl ; cout << "cr:␣" << typeid ( cr ) . name () << endl ; } 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 Person { std : : s t r i n g name_ ; i n t age_ ; p u b l i c : Person ( std : : 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 here ? b = a ; // And here ? } Florian Richoux How to master C++ 12 / 57

Copy constructor and copy assignment operator // 1. copy c o n s t r u c t o r Person ( const Person& that ) : name_( that . name_) , age_ ( that . age_ ) { } // 2. copy assignment o p e r a t o r Person& operator=(const Person& that ) { name_ = that . name_ ; age_ = that . age_ ; return ∗ t h i s ; } Signature Classname ( const Classname& ) // copy c t o r Classname& operator=( const Classname& ) // assignment op Florian Richoux How to master C++ 13 / 57

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 r b = a ; // C a l l the copy assignment o p e r a t o r Person c = a ; // ? } Florian Richoux How to master C++ 14 / 57

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 r b = a ; // C a l l the copy assignment o p e r a t o r Person c = a ; // C a l l the copy c t o r // ( almost 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

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 // ////////////// void foo ( 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 simple t h i n g to avoid t h i s ? // ////////////// MyClass foo () { MyClass temp ; . . . return 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

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 ; // copy assignment op b = MyClass ( a ) ; // copy c t o r + copy assignment op MyClass ∗a = new MyClass ( ) ; // c o n s t r u c t o r MyClass ∗b ; // nothing i s c a l l e d b = a ; // s t i l l nothing i s c a l l e d b = 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

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 { p u b l i c : A() { i = new i n t ;} i n t ∗ i ; }; A a ; A b = a ; // same s t o r y with j u s t b = a std : : cout << a . i << std : : endl << b . i << std : : endl ; Florian Richoux How to master C++ 19 / 57

Why writing a copy ctor and a copy assignment operator? c l a s s A { p u b l i c : A() { i = new i n t ;} i n t ∗ i ; }; A a ; A b = a ; // same s t o r y with j u s t b = a std : : cout << a . i << std : : endl << b . i << std : : endl ; 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 { p u b l i c : A() { i = new i n t ;} // c t o r A( const A& other ) { // copy c t o r i = new i n t ; ∗ i = ∗( other . i ) ; } i n t ∗ i ; }; A a ; A b = a ; // same s t o r y with j u s t b = a std : : cout << a . i << std : : endl << b . i << std : : endl ; Florian Richoux How to master C++ 20 / 57

Why writing a copy ctor and a copy assignment operator? c l a s s A { p u b l i c : A() { i = new i n t ;} // c t o r A( const A& other ) { // copy c t o r i = new i n t ; ∗ i = ∗( other . i ) ; } i n t ∗ i ; }; A a ; A b = a ; // same s t o r y with j u s t b = a std : : cout << a . i << std : : endl << b . i << std : : endl ; 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 { p u b l i c : MyClass ( std : : size_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& other ) // copy c t o r : s i z e ( other . 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 ) { std : : copy ( other . array , other . a r r a y + s i z e , a r r a y ) ; } p r i v a t e : std : : size_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 { . . . p u b l ic : MyClass& operator=(const MyClass& other ) // copy asgmt op { i f ( t h i s != &other ) { // put i n the new data . . . std : : size_t newSize = other . s i z e ; i n t ∗ newArray = newSize ? new i n t [ newSize ] : n u l l p t r ; std : : copy ( other . array , other . a r r a y + s i z e , newArray ) ; // . . . and get r i d of the old data delete [ ] a r r a y ; s i z e = newSize ; a r r a y = newArray ; } return ∗ t h i s ; } } ; Florian Richoux How to master C++ 23 / 57

The copy-and-swap idiom c l a s s MyClass { . . . p u b l ic : MyClass& operator=(const MyClass& other ) // copy asgmt op { i f ( t h i s != &other ) // o f t e n u s e l e s s { // put i n the new data . . . std : : size_t newSize = other . s i z e ; i n t ∗ newArray = newSize ? new i n t [ newSize ] : n u l l p t r ; std : : copy ( other . array , other . a r r a y + s i z e , newArray ) ; // ( th es e 3 l i n e s are code d u p l i c a t i o n ) // . . . and get r i d of the old data delete [ ] a r r a y ; s i z e = newSize ; a r r a y = newArray ; } return ∗ t h i s ; } } ; Florian Richoux How to master C++ 24 / 57

The copy-and-swap idiom c l a s s MyClass { . . . p u b l ic : void swap ( MyClass& other ) { std : : swap ( this −>s i z e , other . s i z e ) ; std : : swap ( this −>array , other . a r r a y ) ; } MyClass& operator=(MyClass other ) // no r e f e r e n c e ! { swap ( other ) ; 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. defined explicitly, then it is likely to need all three of them. Put in other words If your class manages resources, you need to explicitly define: 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 do a copy assignment operator? It copies a new state (copy ctor), and it deletes the old state (destructor). http://stackoverflow.com/questions/4172722/ what-is-the-rule-of-three Florian Richoux How to master C++ 28 / 57

The Rule of Three The rule “A delete for each new” is not sufficient! c l a s s A { p u b l ic : A( i n t i ) : array_ ( i ? new i n t [ i ] : n u l l p t r ) { } ~A() { delete [ ] 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 ) ; . . . 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 sufficient! c l a s s A { p u b l ic : A( i n t i ) : array_ ( i ? new i n t [ i ] : n u l l p t r ) { } ~A() { delete [ ] 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_ delete a1 ; delete a2 ; // Undefined 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 Vector { i n t ∗ storage_ ; size_t size_ ; p u b l i c : // c t o r Vector ( size_t numElements ) : storage_ (new i n t [ numElements ] ) , size_ ( numElements ) { } // dtor ~Vector () { delete [ ] storage_ ; } }; 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 Vector c = a + b ; ??? operator+ ( Vector const & a , Vector 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 Vector c = a + b ; ??? operator+ ( Vector const & a , Vector 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 Vector c = a + b ; ??? operator+ ( Vector const & a , Vector 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 Vector c = a + b ; ??? operator+ ( Vector const & a , Vector 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? Vector operator+ ( Vector const& a , Vector const& b ) { // c r e a t e r e s u l t of same s i z e a s s e r t ( a . s i z e () == b . s i z e ( ) ) ; Vector r e s u l t ( a . s i z e ( ) ) ; // compute a d d i t i o n std : : transform ( a . begin ( ) , a . end ( ) , // in p u t 1 b . begin ( ) , // in p u t 2 r e s u l t . begin ( ) , // r e s u l t std : : plus <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 std : : s t r i n g f ( bool cond = f a l s e ) { std : : s t r i n g f i r s t ( "first" ) ; std : : s t r i n g second ( "second" ) ; return 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

C++11 and move semantics Transferring value into a scope Ray computeRay () { Vector o r i g i n ; Vector 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 Vector : : Vector ( Vector&& other ) // s h a l l o w copy : storage_ ( other . storage_ ) , size_ ( other . size_ ) { // n u l l i f y source other . storage_ = n u l l p t r ; other . size_ = 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 () { Vector o r i g i n ; Vector d i r e c t i o n ; . . . return Ray ( std : : move ( o r i g i n ) , // moved ! std : : 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 + define the move ctor (+ define 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 declared 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 declared 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 stuff) C++ cast Functors C++11 features like: auto, lambda, decltype C++14 C++17 Florian Richoux How to master C++ 45 / 57

Safety Asserts Use assert. Unable them with the -NDEBUG 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

Read! Books Efficient 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 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 modification 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, mercury, ... 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, mercury, ... 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 differences 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 difference 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 differences 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 difference 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 difference between #include <iostream.h> and #include <iostream>? What’s the difference 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

Add a comment

Related presentations

Related pages

www.mastercard.com

Hier sollte eine Beschreibung angezeigt werden, diese Seite lässt dies jedoch nicht zu.
Read more

Den C€ PostMaster. - Control€xpert: Startseite

Unser PostMaster® ist ein sicheres webbasiertes Übermittlungsverfahren für die schnelle Kommunikation zwischen allen Beteiligten im Schadenprozess.
Read more

Master – Wikipedia

Der Master of Arts ist üblich in den Geisteswissenschaften und Sozialwissenschaften und kann an einen Bachelor of Arts ... a b c a b; Literatur. S ...
Read more

NC-Tabelle für Studiengänge mit dem Abschlussziel Master

NC-Tabelle für Studiengänge mit dem Abschlussziel Master. Die Auswahlergebnisse des letzten Semesters für die an der Freien Universität Berlin angebotenen
Read more

MASTER-C - Software Informer. MASTER-C is a school ...

MASTER-C (Master-C.exe). MASTER-C is a school management application that is used by over 500 schools in rural and urban areas. The system is fully ...
Read more

Mastercard | A World Beyond Cash | Leader in Global ...

Mastercard is a leader in global payments and a technology company that connects billions of consumers, thousand of financial institutions, and millions of ...
Read more

MasterCard Karten & Zahlungslösungen für Kunden

Mastercard bietet Verbraucherkredite, Prepaid- und Debitkarten, die es schneller und einfacher machen, mit mehr Sicherheit alles und überall einzukaufen.
Read more

Hebetechnik Heymer - CargoMaster C

Beim häufigen Transport schwerer Lasten, die teilweise sogar das eigene Körpergewicht übersteigen, kann es zu Unfällen und Gesundheitsschäden kommen.
Read more

Clean Master für PC - Download - CHIP

Clean Master für PC 1.0 Build 11.3.1013 Englisch: Clean Master ist ein unter Android sehr beliebtes Putztool. Jetzt ist eine PC-Umsetzung für Windows ...
Read more

Master of Science (M.Sc.) – Verlauf, Voraussetzungen und ...

Der Master of Science ist der 2. höchste akademische Hochschulabschluss. Informiere Dich hier über Voraussetzungen, Verlauf und Karriereaussichten.
Read more