explicit关键字

加上该关键字就不允许隐式转化了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class point{
public:
//explicit
Point(int x = 0,int y = 0)
:_ix(x)
,_iy(y)
{
}
private:
int _ix;
int _iy;
};

void test(){
Point p = 1;//其实类似与 ClassName cn = "hello"
//但是上面加上了explicit就不允许这样使用了
}

文件输入流的无参构造函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#inlucde <fstream>
ifstream ifs;//使用无参构造函数需要在调用open函数打开文件
ifs.open("test.cc");

//ifs本身也可以当作判断条件,如果打开成功就是good状态,也就是true
if(!ifs){
cerr << "ifstream open filed!" << endl;
return;
}
//从文件中获取内容,将文件中的内容输入到内存中
string world;
ifs >> word;
cout << word << endl;