-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathExplicit.cpp
More file actions
51 lines (40 loc) · 1.3 KB
/
Explicit.cpp
File metadata and controls
51 lines (40 loc) · 1.3 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
// =====================================================================================
// Explicit.cpp // Keyword 'explicit'
// =====================================================================================
module modern_cpp:explicit_keyword;
namespace KeywordExplicit {
class Complex {
private:
double m_real;
double m_imag;
public:
// c'tors
Complex() : Complex {0.0, 0.0}{}
/* explicit */ Complex(double real) : // remove or add keyword 'explicit'
m_real{ real }, m_imag{} {}
Complex(double real, double imag) :
m_real{ real }, m_imag{ imag } {}
// comparison operator
bool operator == (Complex rhs)
{
return (m_real == rhs.m_real && m_imag == rhs.m_imag);
}
};
}
void main_explicit_keyword()
{
using namespace KeywordExplicit;
// a Complex object
Complex c{ 3.0, 0.0 };
if (c == 3.0) // remove or add cast '(Complex)'
{
std::cout << "Same objects" << std::endl;
}
else
{
std::cout << "Not same objects" << std::endl;
}
}
// =====================================================================================
// End-of-File
// =====================================================================================