声明为explicit的构造函数更受欢迎,它禁止编译器执行非预期的类型转换
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
class B
{
public:
explicit B(int a = 1){}
};
class C
{
public:
C(int a = 1){}
};
void dob(B b){}
void doc(C c){}
int main()
{
B b;
dob(B(1));
doc(1);
dob(1);
//编译错误,B类型不能接受隐式转换
}
size_t是被typedef出来的unsigned类型
#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
size_t a = 0;
cout << a << endl;
a--;
cout << a << endl;
}