-
Notifications
You must be signed in to change notification settings - Fork 49
Expand file tree
/
Copy pathWhy_Initialization_List.cpp
More file actions
43 lines (34 loc) · 1.46 KB
/
Why_Initialization_List.cpp
File metadata and controls
43 lines (34 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <iostream>
#include <cmath>
using namespace std;
/*
Why do we need an initialization list in constructors in C++ ?
Ans:
(1) The life time of an object start exactly at the starting point of the body of constructor.
So, the moment the control reaches this point, the object construction is considered complete.
If we put the value inside the body, then at the time the life time of object starts, the data members
will have inconsistent value.
It will not matter in examples as shown below, but there are examples where it does really matter whether you have
properly initialized members or not when the object life time starts.
(2) There can be n data members that can be initialized. If we initialize them inside constructor body,
compiler can initialize them in n! ways (there can be a case when initialization of one member depends on some other
member. But when we do initialization using initializer list, compiler initializes them in one unique way
which depend on which member comes first in definition of class.
*/
class Stack {
private:
char *data_; //it will be initialized first in initialization list
int top_; //then this will be initialized
public:
Stack(): data_(new char[10]), top_(-1) {}
~Stack();
};
Stack::~Stack() {
cout << "Destructor called\n";
delete data_;
}
//Application
int main() {
Stack s;
return 0;
}