聘我网

新概念招聘3.0

c++中explicit的作用是什么?

vote up0vote downstar
explicit CImg(const char *const filename):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
  assign(filename);
}

上面explicit的作用是什么?

 

1 个答复

vote up1vote downcheck

explicit是构造函数的decorator,使构造函数不能被编译器用作implicit conversions

C++允许用户提供的conversion -- 通过构造函数:

 class circle {
   circle( const int r ) ;
 }

 circle c = 3 ; 

这里circle c = 3 ;等效于circle c(3);,如果不想要这个特性,可以用explicit

 class circle {
   explicit circle( const int r ) ;
 }
链接

您的回答





不是您要找的问题? 浏览其他含有标签 的问题或者 自己问个.