-
Notifications
You must be signed in to change notification settings - Fork 8.5k
/
explicit.cpp
92 lines (76 loc) · 2.15 KB
/
explicit.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
//
// Created by light on 19-11-3.
//
#include <iostream>
using namespace std;
// non-explicit-one-argument ctor
class Fraction
{
public:
// non-explicit-one-argument ctor 可以把别的东西转换为Fraction这种
Fraction(int num, int den = 1) : m_numerator(num), m_denominator(den) {}
Fraction operator+(const Fraction &f)
{
return Fraction(this->m_numerator + f.m_numerator);
}
void getNumAndDen()
{
cout << m_numerator << " " << m_denominator << endl;
}
private:
int m_numerator; // 分子
int m_denominator; // 分母
};
// explicit-one-argument ctor
class Fraction1
{
public:
// explicit-one-argument ctor 可以把别的东西转换为Fraction这种
explicit Fraction1(int num, int den = 1) : m_numerator(num), m_denominator(den) {}
Fraction1 operator+(const Fraction1 &f)
{
return Fraction1(this->m_numerator + f.m_numerator);
}
void getNumAndDen()
{
cout << m_numerator << " " << m_denominator << endl;
}
private:
int m_numerator; // 分子
int m_denominator; // 分母
};
class P
{
public:
P(int a, int b)
{
cout << "P(int a,int b)\n";
}
P(int a, int b, int c)
{
cout << "non-explicit P(int a,int b,int c)\n";
}
explicit P(int a, int b, int c, int d)
{
cout << "explicit P(int a,int b,int c,int d)\n";
}
};
void fp(const P &)
{
}
int main()
{
Fraction f(3, 5);
Fraction ff = f + 3; // 会将3转换为(3,1) 隐式转换
ff.getNumAndDen();
// 如果不想编译器进行隐式转换,可以在前面添加explicit关键字
// c++2.0之前explicit只禁止"存在单一实参"转换
// c++2.0之后explicit可以进制"多个实参"转换
// Fraction1 f1(3,5);
// Fraction1 f2 = f1+3; // 会将3转换为(3,1) 不会隐式转换 error: no match for ‘operator+’ (operand types are ‘Fraction1’ and ‘int’)
// f2.getNumAndDen();
P p1 = {77, 5};
P p2 = {77, 5, 89};
// P p3 = {77, 5, 89,99}; //error: converting to ‘P’ from initializer list would use explicit constructor ‘P::P(int, int, int, int)’
return 0;
}