[Dev-C++] function overloading ambiguity
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: <or...@vp...> - 2004-04-30 21:26:53
|
Hi!
I encountered a kind of "stupidity", which took me quite some time to=20
debug in my original program. I had 3 functions, same name, they were=20
overloaded.
1. function's parameter is: const string&
2. function's parameter is: int
3. function's parameter is: bool
Here is a demonstration of my problem:
--- Code ---
#include <iostream>
#include <string>
using namespace std;
void Test( const string& str )
{
cout << "Function called, type: 'string'" << endl;
cout << " Value:" << str << endl;
}
void Test( int i )
{
cout << "Function called, type: 'int'" << endl;
cout << " Value:" << i << endl;
}
void Test( bool b )
{
cout << "Function called, type: 'bool'" << endl;
cout << " Value:" << b << endl;
}
int main()
{
Test( "This is some text" );
Test( 12345 );
Test( true );
=09
return 0;
}
--- Code End ---
And this is the output:
Function called, type: 'bool'
Value:1
Function called, type: 'int'
Value:12345
Function called, type: 'bool'
Value:1
A const char* was converted to a bool rather than to a const string&=20
without a blink (i.e. without ANY warnings, even with -Wall and -W).
Shouldn't at least the compiler spit out something, that "I'll convert=20
your text to a bool"-like warning? :)
I know, it sees char* as a pointer, and it seems easier for it to=20
convert to bool, rather than to instanciate a string object, but why=20
aren't there any *warnings* (about ambiguity)?
It made me go crazy until I found this out...
I later changed the first one to take const char* instead.
Is it related to built-in types (conversion between them is forced if=20
possible)?
Any thoughts?
--
Greetings,
Bal=E1zs
|