explicit for ctors taking one argument
我们看一组例子 有两个复数类 Complex 与Complex2 他们的唯一区别就是
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
| struct Complex { int real,image; Complex(int a, int b=0) : real(a), image(b) { } Complex operator+(const Complex &x) { return Complex(real + x.real, image + x.image); } };
struct Complex2 { int real,image; explicit Complex2(int a, int b=0) : real(a), image(b) { } Complex2 operator+(const Complex2 &x) { return Complex(real + x.real, image + x.image); } }; ```
```cpp int main() { Complex c1(12,4); Complex c2 = c1 + 5; Complex2 c3(12,4); Complex2 c4 = c3 +5; return 0; }
|
explicit for ctors taking more than one argument (c++11)