-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInitializeVariable.cpp
More file actions
65 lines (55 loc) · 2.03 KB
/
InitializeVariable.cpp
File metadata and controls
65 lines (55 loc) · 2.03 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <iostream>
using namespace std;
void initialize_variable();
// A struct that runs code when its object is created
struct InitializeVariable
{
InitializeVariable()
{
cout << "\n"
<< "\n"
<< "InitializeVariable\n";
initialize_variable();
}
};
// All global and static objects are constructed before main() begins.
static InitializeVariable autoRunInstance;
struct Foo
{
Foo()
{
cout << "Default constructor/ default init\n";
}
// Foo(int)
explicit Foo(int)
{
cout << "Constructor called with int / copy init\n";
}
Foo(const Foo &other)
{
std::cout << "Copy constructor called\n";
}
};
void initialize_variable()
{
cout << "\n--- Variable Initialization Examples ---\n";
// There are there common ways to intialize a variable
// * Default
[[maybe_unused]] int initDefaultVar;
[[maybe_unused]] Foo initDefaultObj;
// * Traditional initialization
// * Copy-init: Type var = value;
// 1. Compiler tries to convert the value to a temporary Foo.
// 2. If the constructor is explicit, implicit conversion is blocked -> compilation error.
// 3. Otherwise, a temporary Foo is created and then copied/moved into the variable.
[[maybe_unused]] Foo copyInitObj = (Foo)2.3; // explicit cast: double 2.3 -> int 2 (implicit narrowing) -> Foo(int) -> temporary Foo -> copied/moved into copyInitObj
// Foo copyInitObjError = 2.3; // ERROR: implicit conversion blocked by explicit constructor
// We can explicitly prevent certain conversions using = delete or using {}
// * direct-init: Type var(value);
[[maybe_unused]] Foo directInitObj(4); // call Foo(int)
[[maybe_unused]] Foo directInitObj2(4.3); // look for constructor -> implicit 4.3(float) -> 4(int) -> call Foo(int) ->
// * Brace init
// calls the constructor directly without allowing implicit conversions.
[[maybe_unused]] Foo braceInit{3};
// Foo braceInit2{3.3}; // ERORR => Prefer this way
}