Function overloading

S

Simon Elliott

#include <iostream>
#include <string>

static void Test(const std::string& t)
{
std::cout << "std::string: " << t <<std::endl;
}

static void Test(bool t)
{
std::cout << "bool: " << t <<std::endl;
}


int main(int argc, char* argv[])
{
std::string t1 = "t1";
bool t2 = true;
char t3[] = "t3";

std::cout << "std::string" <<std::endl;
Test(t1);

std::cout << "bool" <<std::endl;
Test(t2);

std::cout << "char array" <<std::endl;
Test(t3);

return 0;
}

Output:

std::string
std::string: t1
bool
bool: 1
char array
bool: 1

Why does a call to Test with a char array map to
static void Test(bool t)?

I'd have expected it to either map to
static void Test(const std::string& t)

or give a compile time error.
 
H

Howard

Simon Elliott said:
#include <iostream>
#include <string>

static void Test(const std::string& t)
{
std::cout << "std::string: " << t <<std::endl;
}

static void Test(bool t)
{
std::cout << "bool: " << t <<std::endl;
}


int main(int argc, char* argv[])
{
std::string t1 = "t1";
bool t2 = true;
char t3[] = "t3";

std::cout << "std::string" <<std::endl;
Test(t1);

std::cout << "bool" <<std::endl;
Test(t2);

std::cout << "char array" <<std::endl;
Test(t3);

return 0;
}

Output:

std::string
std::string: t1
bool
bool: 1
char array
bool: 1

Why does a call to Test with a char array map to
static void Test(bool t)?

I'd have expected it to either map to
static void Test(const std::string& t)

or give a compile time error.

I think I know this one. (I could be wrong. It happened once or twice
before. :))

The implicit conversion of a char array to a bool takes place because a char
array can be seen as a pointer (to the first character of the array). And a
pointer can be implicitly converted to a bool in order to allow testing for
NULL with statements like "if (ptr) {...}". So since there is an available
conversion, the compiler uses it.

It won't try to convert to std::string, because the types are unrelated.
There is an operator = and a constructor for std::string which take a char
array (pointer), but neither of those is used here. If you want to call the
string version, then you need to first construct a string using that array
and then pass the string.

If that's not the function you wanted to call, then what exactly did you
_want_ to happen? (Not a compile error, I assume? :))

-Howard
 
P

Puppet_Sock

Simon Elliott wrote:
[code sample snipped]
Why does a call to Test with a char array map to
static void Test(bool t)?

I'd have expected it to either map to
static void Test(const std::string& t)

or give a compile time error.

I get a compiler warning about converting char *
to bool, and it says it's a performance issue.
Socks
 
?

=?ISO-8859-1?Q?Daniel_Sch=FCle?=

because this would requre 2 conversions

#include <iostream>

void xxx(char x){ std::cout << "xxx = " << x << std::endl; }

class foo
{
char c;
public:
foo(char c_) : c(c_){}
operator char() {return c;}
};

class bar
{
foo f;
public:
bar(foo f_) : f(f_){}
operator foo() { return f; }
};

int main()
{
bar b(foo('A'));
// xxx(b); // 2 conv. required: b->foo->char
xxx(static_cast<foo>(b)); // only one
return EXIT_SUCCESS;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top