B::B(int x, bool b) { cout << x << ": " << sizeof(x) << endl; cout << b << ": " << sizeof(b) << endl; }
int main() { B exp1 = 10; return 0; }
如果加上了explicit关键字,编译时就会报错 “pre.cpp: In function ‘int main()’: pre.cpp:15:14: error: conversion from ‘int’ to non-scalar type ‘B’ requested B exp1 = 10;”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#include <iostream> using namespace std;
class B { public: explicit B(int x = 0, bool b = true); };
B::B(int x, bool b) { cout << x << ": " << sizeof(x) << endl; cout << b << ": " << sizeof(b) << endl; }