On Fri, Jul 27, 2007 at 09:02:42AM -0500, Jonathan Cast wrote: > On Friday 27 July 2007, Jon Fairbairn wrote: > > ChrisK <[EMAIL PROTECTED]> writes: > > > Because this is starting to sound like one of the > > > maddening things about C++. > > > > > > Namely, the automatic implicit casting conversions of > > > classes via their single argument constructors. > > > > Unfortunately I'm not sufficiently familiar with C++ to know > > what this means. Perhaps you could clarify? > > Somebody noticed that, in C, you could mix integers and floats (almost) > freely, and in Classic C, you could mix pointers and integers freely, and > thought this was /such/ a wonderful idea that C++ has special syntax to > declare the conversion functions allowing you to, say, mix pointers and > pointer-like classes freely, or to mix char*s and strings freely, etc. It's > what makes
To give a somewhat more mundane example if you define a class Array
class Array {
public:
Array(int); // ... construct a new array of specified length
...
}
Then if you make the mistake of passing an integer constant to a function
that expects an Array, C++ will happily construct a new Array of that size
and pass that to the function.
Even more exciting when you use overloading: if you define multiplication
between two Arrays, then if you accidentally try to multiply an Array by an
integer constant (thinking it'll be a scalar multiply), then a new Array of
that size will be constructed and multiplied--almost certainly resulting in
a runtime error (mismatched Array sizes), but certainly not what you want.
The solution is to add explicit to the constructor for all single-argument
constructors (except perhaps occasionally when you actually want explicit
construction of objects).
The reasoning behind this, of course, is to allow nice interactions of
home-made classes such as complex numbers, or string classes (which you
might want to be automatically constructed from string constants).
--
David Roundy
Department of Physics
Oregon State University
signature.asc
Description: Digital signature
_______________________________________________ Haskell-Cafe mailing list [email protected] http://www.haskell.org/mailman/listinfo/haskell-cafe
