1 /*
2 *C++ explicit 关键字
3 *
4 *It prevents constructor class from being used to perform implicit type conversion
5 *
6 *Author: MagicYun star3012@qq.com
7 *
8 */
9
10
11 #include <iostream>
12 using namespace std;
13
14 class Student
15 {
16 public:
17 Student(int age){};
18 ~Student(){};
19 };
20
21 class Animal
22 {
23 public:
24 explicit Animal(float weight){};
25 ~Animal(){};
26 };
27
28 int main()
29 {
30 Student Hui(110); //fine
31 Student Liang = 140; //fine
32
33 Animal pig(180.0); //fine
34 //Animal bird = 1.8; //error: conversion from `double' to non-scalar type `Animal' requested
35 return 0;
36 }
2 *C++ explicit 关键字
3 *
4 *It prevents constructor class from being used to perform implicit type conversion
5 *
6 *Author: MagicYun star3012@qq.com
7 *
8 */
9
10
11 #include <iostream>
12 using namespace std;
13
14 class Student
15 {
16 public:
17 Student(int age){};
18 ~Student(){};
19 };
20
21 class Animal
22 {
23 public:
24 explicit Animal(float weight){};
25 ~Animal(){};
26 };
27
28 int main()
29 {
30 Student Hui(110); //fine
31 Student Liang = 140; //fine
32
33 Animal pig(180.0); //fine
34 //Animal bird = 1.8; //error: conversion from `double' to non-scalar type `Animal' requested
35 return 0;
36 }