Suggestion: make all one-parameter constructors explicit to avoid behind-the-scenes type conversions.
Why?
In C++, a one parameter constructor defines an implicit type conversion, in which a temporary object is created that makes an assignment (or parameter to a function) compatible.
IntCell obj;
obj = 37;
will be transformed into:
IntCell temporary = 37; //initialization, one-parameter constructor will be invoked
obj = temporary; //assignment
The construction of the temporary can be performed by using the one-parameter constructor. The use of explicit means that a one-parameter constructor cannot be used to generate an implicit temporary.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment