explicit CImg(const char *const filename):_width(0),_height(0),_depth(0),_spectrum(0),_is_shared(false),_data(0) {
assign(filename);
}
what's the difference with or without it?
Answer
It's use to decorate constructors; a constructor so decorated cannot be used by the compiler for implicit conversions.
C++ allows up to one user-provided conversion, where "user-provided" means, "by means of a class constructor", e.g., in :
class circle {
circle( const int r ) ;
}
circle c = 3 ; // implicit conversion using ctor
the compiler will call the circle ctor here, constructinmg circle c
with a value of 3 for r
.
explicit
is used when you don't want this. Adding explicit means that you'd have to explicitly construct:
class circle {
explicit circle( const int r ) ;
}
// circle c = 3 ; implicit conversion not available now
circle c(3); // explicit and allowed
No comments:
Post a Comment