DEV Community

Discussion on: C++ Programming: Implicit and Explicit Constructors

Collapse
 
aminmansuri profile image
hidden_dude • Edited

Yes C++ has always allowed implicit "casting" of values. If a suitable constructor has been found.

That is both a blessing and a curse.

Of course it's up to you to design in what your class should or should not have.

Collapse
 
julienbdubois profile image
JBD

It is more a curse than a blessing to me, there are really few relevant cases where an implicit case can be considered, most of the time, it’s not. std::string::string(char const*) is typically a case where the implicit constructor hide a whole copy of the string and a potential heap allocation.
Implicit things should be trivial, non-trivial things should be explicit.

Collapse
 
grave18 profile image
Grave

Yes, and most static analyzers advise you to add explicit keyword to constructors

Collapse
 
aminmansuri profile image
hidden_dude

Yeah.. C++ can be like that.

Effective C++ covers all these odd cases. The book could also have been called "50 ways C++ can have hidden memory leaks".