-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathexplicit_constructor.cpp
More file actions
executable file
·60 lines (53 loc) · 1.87 KB
/
explicit_constructor.cpp
File metadata and controls
executable file
·60 lines (53 loc) · 1.87 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
// Explicit Constructor
//
// The compiler is allowed to make ONE implicit conversion to resolve the
// arguments of a function/constructor. A single-argument constructor that is
// not marked `explicit` therefore doubles as an implicit conversion from its
// parameter type. This is often surprising. The `explicit` keyword disables
// that conversion: the constructor can still be called, but only directly.
#include <iostream>
namespace Implicit {
class String {
public:
// allocate n bytes to the String object
String(int n) {
std::cout << " allocate " << n << " bytes to the String object"
<< std::endl;
}
// initialize object with char *p
String(const char *p) {
std::cout << " initialize object with char *p" << std::endl;
}
};
} // namespace Implicit
namespace Explicit {
class String {
public:
// allocate n bytes
explicit String(int n) {
std::cout << " allocate " << n << " bytes to the String object"
<< std::endl;
}
// initialize object with char *p
String(const char *p) {
std::cout << " initialize object with char *p" << std::endl;
}
};
} // namespace Explicit
int main() {
std::cout << "--- Implicit conversion trap ---" << std::endl;
// 'a' is a char, implicitly convertible to int (ASCII 97).
// Copy-initialization picks String(int) and allocates 97 bytes,
// which is almost certainly not what the caller meant.
Implicit::String s1 = 'a';
std::cout << "--- explicit disables the trap ---" << std::endl;
// With `explicit String(int)`, the following line no longer compiles:
//
// Explicit::String s2 = 'a'; // error: copy-init cannot use
// // explicit constructor
//
// We must convert the char ourselves. Casting to const char* selects
// the String(const char *) constructor instead.
Explicit::String s2 = (const char *)'a';
return 0;
}