help-gplusplus
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Default parameters - problems


From: John V. Shahid
Subject: Re: Default parameters - problems
Date: Sat, 16 Jun 2007 00:51:53 -0400

On Sat, 2007-06-16 at 09:47 +0530, anitha boyapati wrote:
> Hi John,
> 
> > > using namespace std;
> > > class test {
> > >       public :
> > >               test() { };
> >
> > I was surprised that g++ compiled your code (after removing the first
> > function and leaving the overloaded one) although you have a semicolon
> > after the function test(), which is something I've never seen before and
> > thought was invalid !!!
> 
>   Its a pretty much common pratice in the enviroment I work - although
> an inelegant one.
>   But why should it not work ?

Simply because it doesn't confirm with the C++ standard, as far as I
know.

>  I didnot think about static_cast<bool> or casting either for that matter.

I'm sorry about the above statement, it was a kind of misleading.
Usually, conversions that can be done using explicit casting and not
implicitly don't govern the compiler decisions of which function to
call. The problem here is that the conversion can be done implictly
without an explicit casting. According to Stroustrup's C++ book, "A
pointer can be implicitly converted to a bool (§C.6.2.5). A nonzero
pointer converts to true and a zero valued pointers converts to false."
Which is also why the program will compile if you removed the overloaded
version of the function.

Note: The above statement about pointers hold true for integer types. A
nonzero integer value can be assigned to a boolean variable as a true
and a zero variable becomes false. This behavior is very important so
that old C code (i.e. using a pointer in the if conditional statement)
can be compiled successfully under the C++ compilers despite its strict
type checking.
-- 
John V. Shahid <jvshahid@gmail.com>





reply via email to

[Prev in Thread] Current Thread [Next in Thread]