Op 29-10-2010 16:22, Richard Heck schreef:
On 10/29/2010 09:20 AM, Uwe Stöhr wrote:
> With classes, this happens whenever one class has a constructor
that > takes some other type as an argument. So, for example, suppose we
> have:
>
> class A {
> A();
> A(int);
> };
>
> So class A has two constructors: A default one, and one that takes
an int.
This feature was confusing me when I started with LyX and thus with
C++. I learned Pascal and ObjectPascal (a.k.a. Delphi) at school.
This language only allows one constructor and I was taught that this
is much safer and that one should also not use more than one
constructor in other programming languages. This statement was
obviously incorrect.
Implicit conversions can be quite dangerous, and catch you unawares. I
can't think of a good example at the moment, but the point is just
that they essentially prevent type-checking.
Here's a silly example, extending the previous. Suppose you have:
int i;
A I;
Now you do:
A newa = i; # typo!!
But the compiler doesn't catch it, because of the implicit conversion.
Richard
Well a good example is if you look at enumerations:
enum Number {
RandomEvent = 0,
QuiteUnrelatedScience = 1
}
Noone prevents you from writing
Number number = getSomeNumber();
if (number)
do something;
It would be much more safe if you really had to check number for being
the first or the second like
if (number == RandomEvent)
do something
or
switch (number)
{
case RandomEvent:
do_something();
break;
case QuiteUnrelatedScience:
// do nothing
}
Vincent