-
Notifications
You must be signed in to change notification settings - Fork 12
/
{}-operator-aggregate-copy-default-direct-value-zero.cpp
53 lines (43 loc) · 1.32 KB
/
{}-operator-aggregate-copy-default-direct-value-zero.cpp
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
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <map>
#include <string>
#include <vector>
class Foo {
public:
int i;
};
class student {
public:
int id;
std::string name;
student(int id, std::string name) : id{id}, name{name} {
std::cout << "id :" << id << " name is: " << name << std::endl;
}
};
int main() {
char Array1[5] = {}; // char Array2[5] = {'\0', '\0', '\0', '\0', '\0'}
char *Array2[5] =
{}; // char * Array3[5] = {nullptr, nullptr, nullptr, nullptr, nullptr};
int i{}; // i becomes 0
std::string s{}; // s becomes ""
std::vector<float> v{}; // v becomes an empty vector
double d{}; // d becomes 0.0
std::vector<int> vec1{1, 2, 3, 4, 5};
std::map<std::string, int> myMap{{"Scott", 1976}, {"Dijkstra", 1972}};
/*
We can use that to initialize class member variables and constructors as
well member initialization list.
*/
// Initializations using the constructor
student first_student(10, "mumbo");
student second_student{11, "jumbo"};
std::cout << "value initialization" << std::endl;
Foo *inst3 = new Foo();
std::cout << inst3->i << std::endl;
std::cout << "copy-initialization" << std::endl;
Foo inst4 = Foo();
std::cout << inst4.i << std::endl;
std::cout << "direct-initialization" << std::endl;
Foo inst5((Foo()));
std::cout << inst5.i << std::endl;
}