Friday, 15 February 2013

c++ - Why doesn't the explicit specifier on a constructor prevent upcasts? -



c++ - Why doesn't the explicit specifier on a constructor prevent upcasts? -

i'm misunderstanding explicit specifier. why next code compile?

struct { }; struct b : { }; struct c { a* a_; explicit c(a* a) : a_(a) { } }; int main() { b b; c c(&b); }

the base of operations class c constructor called b* implicitly upcast a*. thought explicit specifier intended prevent implicit conversion of arguments?

i'm asking because understand practice add together explicit specifier single argument constructors , want check compiler not letting me shouldn't doing.

explicit prevents constructor itself beingness used implicit conversion other expression. doesn't prevent employing implicit conversions when invoking it.

there's no way prevent compiler performing particular conversion 1 primitive type (and pointers primitive types).

c++ c++11

No comments:

Post a Comment