473,387 Members | 1,425 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Function overloading



#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.

--
Simon Elliott http://www.ctsn.co.uk
Jul 23 '05 #1
3 1679

"Simon Elliott" <Simon at ctsn.co.uk> wrote in message
news:42***********************@news.gradwell.net.. .


#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


Jul 23 '05 #2
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

Jul 23 '05 #3
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;
}

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Doran_Dermot | last post by:
Hi All, I've seen lots of code in which the attributes of a class are accessed and modified using two separate methods. For example: class Problems: def __init__( self, refNum ):...
4
by: Dave Theese | last post by:
Hello all, I'm trying to get a grasp of the difference between specializing a function template and overloading it. The example below has a primary template, a specialization and an overload. ...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
39
by: zeus | last post by:
I know function overloading is not supported in C. I have a few questions about this: 1. Why? is it from technical reasons? if so, which? 2. why wasn't it introduced to the ANSI? 3. Is there any...
45
by: JaSeong Ju | last post by:
I would like to overload a C function. Is there any easy way to do this?
7
by: asdf | last post by:
They looks so similar. Anybody would like to tell me their differences? Thanks a lot.
15
by: lordkain | last post by:
is it possible to do some kind of function overloading in c? and that the return type is different
10
by: subramanian100in | last post by:
The following is a beginner's question. Suppose TYPE1 and TYPE2 are two types for which suitable ctors and operator= are defined. Suppose I have class Test { TYPE1 mem1;
10
by: Matthew | last post by:
Am I correct in thinking there is no method/function overloading of any kind in any version of PHP? Thanks, Matthew
14
by: mesut | last post by:
hi colleagues, I don't know if this is the right group for but it's in C# so I try. I have a #3 procedural function called GetInfo.. and those are 3 overloaded methods. I would like to use the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.