DEV Community

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

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".