0% found this document useful (0 votes)
63 views121 pages

Section Diving Deep Into Constructors and Initialization Slides

Uploaded by

Dheeraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
63 views121 pages

Section Diving Deep Into Constructors and Initialization Slides

Uploaded by

Dheeraj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 121

Slides

Section : Diving deep into


constructors and
initialization
1
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

2
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Diving Deep into Constructors
and Initialization : Intro

3
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constructors

A window to customize how our own class


objects are put together

4
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

5
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Default parameters for
constructors

6
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
7
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Default parameter

8
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Use default arguments

9
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Specify all default parameters

10
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Confusion

11
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

12
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer lists for constructors

13
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
14
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Member wise assignment initialization

15
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer list initialization

16
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer list benefits

• They avoid unnecessary copies. More on this in next lecture


• In some cases, they’re the only way to initialize an object

17
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

18
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer lists VS Member wise
copy initialization

19
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
20
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Member wise assignment initialization

21
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer list initialization

22
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Member wise copy Initializer lists

• Two steps :
• Initialization happens at real
• object creation
object creation
• member variable
• Unnecessary copies avoided
assignment
• Order of member variables
• Potential unnecessary copies of
matters
data
• Order of member variables
doesn’t matter

23
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Recommendation

Always prefer initializer lists over member wise copy initialization.

24
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

25
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Explicit Constructors

26
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One parameter constructor

27
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
One parameter constructor

28
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Sneaky Implicit conversions

29
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Mark single parameter constructor as explicit

30
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Beware of constructors with all but one default parameters

31
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

32
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constructor Delegation

33
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Two separate constructors

34
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Two separate constructors

35
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Delegate object construction

36
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Event sequence

37
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
What if you call the three param constructor yourself

38
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
No further initializations before/after delegation call

39
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Can do anything we want in body though [after delegation call]

40
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

41
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy Constructors

42
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Person

43
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Make a copy

44
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
P1

last_name

fist_name

age

45
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
P2 P1

last_name last_name

fist_name fist_name

age age

46
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Pointer address copied

47
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
It is possible to set up your own copy constructor. This will disable the default
one provided by the compiler. Yours will be the active one.

48
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Our own copy constructor : Attempt 1

49
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Our own copy constructor : Attempt 1 BAD

50
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Our own copy constructor : Attempt 2

51
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Our own copy constructor : Attempt 2 BAD

52
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Our own copy constructor : Attempt 3

53
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Our own copy constructor : Attempt 3 GOOD

54
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Our own copy constructor : Delegating

55
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

56
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Objects stored in arrays create
copies

57
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Array elements are copies

58
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copies in range based for loop

59
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
References avoid copies

60
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Regular loops don’t make copies

61
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

62
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Deep copy VS Shallow copy

63
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shallow copy Deep copy

When pointer member variables


Member wise copy of member are involved, allocating new
variables, even for pointers. memory and copying in data
from the source pointer

64
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
P1

string last_name John

string fist_name Snow

int * age 0x1afd3

content : 25

65
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shallow copy

P2 P1

string last_name John string last_name John

string fist_name Snow string fist_name Snow

int * age 0x1afd3 int * age 0x1afd3

content : 25 content : 25

66
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Deep copy

P2 P1

string last_name John string last_name John

string fist_name Snow string fist_name Snow

int * age 0x1afe4 int * age 0x1afd3

content : 25 content : 25

67
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

68
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Move Constructors

69
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Stealing data from temporary objects

70
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
71
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Building from temporaries : Syntax

72
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
temporary

double * x 0x1afd3

content : 40.7

double * y 0x1afd4

content : 50.3

73
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
p3 temporary

double * x nullptr double * x 0x1afd3

content : none content : 40.7

double * y nullptr double * y 0x1afd4

content none content : 50.3

74
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
p3 temporary

double * x 0x1afd3 double * x 0x1afd3

content : 40.7 content : 40.7

double * y 0x1afd4 double * y 0x1afd4

content : 50.3 content : 50.3

75
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
p3 temporary

double * x 0x1afd3 double * x nullptr

content : 40.7 content : none

double * y 0x1afd4 double * y nullptr

content : 50.3 content : none

76
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Your move constructor

77
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Making sure you have a temporary

78
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

79
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Deleted constructors

80
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Disable a constructor from being used to create objects

81
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
82
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

83
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer list constructors

84
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
85
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
86
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
{1,2,3,4,…}

87
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer list constructor

88
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
std::initializer list 12.45 45.3

89
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Traverse the std::initializer_list

90
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Pointer arithmetic

91
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Store data into member variables

92
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

93
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Aggregate Initialization

94
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
95
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
{1,2,3,4,…}

96
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
All aggregates can be initialized with {}

97
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

98
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Designated Initializers

99
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
100
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

101
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Uniform Initialization for
Aggregates

102
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Uniform Initialization

Initializing any object either through () or {}

103
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
104
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
105
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

106
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Diving Deep into Constructors
and Initialization : Summary

107
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Default parameters

108
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer list initialization

109
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Member wise copy Initializer lists

• Two steps :
• Initialization happens at real
• object creation
object creation
• member variable
• Unnecessary copies avoided
assignment
• Order of member variables
• Potential unnecessary copies of
matters
data
• Order of member variables
doesn’t matter

110
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Explicit Constructors

111
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Constructor Delegation

112
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy constructors GOOD

113
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Copy constructor delegation

114
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Shallow copy

P2 P1

string last_name John string last_name John

string fist_name Snow string fist_name Snow

int * age 0x1afd3 int * age 0x1afd3

content : 25 content : 25

115
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Deep copy

P2 P1

string last_name John string last_name John

string fist_name Snow string fist_name Snow

int * age 0x1afe4 int * age 0x1afd3

content : 25 content : 25

116
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Move constructors

117
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Deleted constructors

118
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Initializer list constructors

119
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
• Aggregate Initialization
• Designated Initializers
• Uniform Initialization for aggregates

120
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya
Slide intentionally left empty

121
The C++ 20 Masterclass : From Fundamentals to Advanced © Daniel Gakwaya

You might also like