help-gplusplus
[Top][All Lists]
Advanced

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

Re: Default parameters - problems


From: anitha boyapati
Subject: Re: Default parameters - problems
Date: Sat, 16 Jun 2007 09:47:28 +0530

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 ?


>
> test.cc: In function `int main()':
> test.cc:11: error: call of overloaded `func1(const char[6], const
> char[6], bool)' is ambiguous
> test.cc:3: note: candidates are: void test::func1(std::string, bool, int)
> test.cc:6: note:  void test::func1(std::string, std::string, bool)
>  Why is this not compiling ? Can somebody explain what is going on here ?
>  Why is it not directly going to overloaded func1 () - declared second. ?



This leaves the second argument, which actually surprised me, that the
compiler get confused whether to convert "world" into a string or a
bool. After a while I realized that "world" is neither a string nor a
bool, instead it is a char array,  which requires the compiler to cast
it to either type, thus the candidates mentioned above. In other words,
if string(const char*) can convert the char* to a string, then certainly
static_cast<bool>() can convert the char* to a bool also. This makes the
compiler confused about which of the two functions is to be called.


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

A workaround to that is to call the overloaded function as follows:
t->func1("hello", string("world"), false);//This will work


 Yes. It worked. Thanks for the information and workaround.


______________________________________________


--
Anitha Boyapati




reply via email to

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