-
Notifications
You must be signed in to change notification settings - Fork 246
/
14.53.cpp
30 lines (24 loc) · 866 Bytes
/
14.53.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
#include <iostream>
class SmallInt {
friend SmallInt operator+(const SmallInt&, const SmallInt&);
public:
SmallInt(int = 0) { } // conversion from int
operator int() const { return val; } // conversion to int
private:
std::size_t val;
};
SmallInt operator+(const SmallInt&, const SmallInt&) {
std::cout << "operator+(const SmallInt&, const SmallInt&)" << std::endl;
return SmallInt();
}
int main() {
SmallInt s1;
//double d = s1 + 3.14; // Error
// both `operator+(const SmallInt&, const SmallInt&)` and
// `built-in operator+(int, double)` are viable functions
double d = static_cast<double>(s1) + 3.14; // OK
// We can use `static_cast` convert `s1` from `SmallInt` to `double`
double d2 = s1 + SmallInt(3.14); // OK, but lose precision
// Or use conversion constructor convert `3.14` from `double` to `SmallInt`
return 0;
}