Board index » cppbuilder » overloaded functions

overloaded functions


2003-10-04 05:10:41 AM
cppbuilder86
I admit this is a programming error because I never overloaded the function
to take paramers int, int, int but I was just curious as to why this is
occuring.
I have a function which is overloaded 3 times. Here are the declarations.
1.) bool __fastcall PlacePlayerInWorld(int PlayerID, AnsiString ipAddress,
bool reduceByPercent = false);
2.) bool __fastcall PlacePlayerInWorld(int PlayerID, const int RealmID,
const int KingdomID, AnsiString ipAddress, bool reduceByPercent = false);
3.) bool __fastcall PlacePlayerInWorld(int PlayerID, const int RealmID,
const int KingdomID, const int ProvinceID, AnsiString ipAddress, bool
reduceByPercent = false);
In code I have
int RealmID = StrToInt(cbRealm->Text);
int KingdomID = StrToInt(cbKingdom->Text);
PlacePlayerInWorld(DM->tblPlayerPLAYER_ID->Value, RealmID, KingdomID);
note that all 3 paramerters are ints
The strange this is that it compiles with out any errors...why?
Second strange thing is that when I step through this code it is actually
invokeing the 1st function which has paramters of int, AnsiString, bool.
The boolean value is by default false. I'm not sure if this is what
confusing the compiler.
This isn't really a big deal I'll just declare another function with 3 ints
for parameters...I just forgot to do it but I'm wondering why the compiler
didn't catch this. I'm using C++ Builder 5.0.
 
 

Re:overloaded functions

"Junk Mail" < XXXX@XXXXX.COM >writes:
Quote
I admit this is a programming error because I never overloaded the function
to take paramers int, int, int but I was just curious as to why this is
occuring.

I have a function which is overloaded 3 times. Here are the declarations.

1.) bool __fastcall PlacePlayerInWorld(int PlayerID, AnsiString ipAddress,
bool reduceByPercent = false);
PlacePlayerInWorld(DM->tblPlayerPLAYER_ID->Value, RealmID, KingdomID);

note that all 3 paramerters are ints

The strange this is that it compiles with out any errors...why?
It finds that the 3 argument version is the only potential candidate
among the overloaded functions, then determines that the conversions
for each argument are "close enough".
An AnsiString has a constructor that takes an int, so you end up
passing in a temporary AnsiString (whose string value is a number
corresponding to RealmID, and the third argument is converted to bool
using the 0==false, anything else == true algorithm.
Quote
Second strange thing is that when I step through this code it is
actually invokeing the 1st function which has paramters of int,
AnsiString, bool. The boolean value is by default false. I'm not
sure if this is what confusing the compiler.
There is no reason it shouldn't compile.
Quote
This isn't really a big deal I'll just declare another function with
3 ints for parameters...I just forgot to do it but I'm wondering why
the compiler didn't catch this. I'm using C++ Builder 5.0.
You didn't do anything wrong, so there wasn't anything to catch.
--
Chris (TeamB);
 

Re:overloaded functions

"Junk Mail" < XXXX@XXXXX.COM >wrote in message
Quote
The strange this is that it compiles with out any errors...why?
Because it matches the first overloaded function. AnsiString has a a
constructor which accepts an 'int', and the C++ language can convert an
'int' to a 'bool'.
Quote
Second strange thing is that when I step through this code it is actually
invokeing the 1st function which has paramters of int, AnsiString, bool.
Correct. That is the only overloaded function you have which can accept
just 3 parameters, and the values are are supplying to it can be
implicitally converted to the types that the function is expecting.
Quote
I'm wondering why the compiler didn't catch this.
It is not an error. It is doing eactly what it is supposed to be doing.
Gambit
 

{smallsort}

Re:overloaded functions

I see you and Chris's point. I guess its just a programming oversite well
ok error :) Thanks for explaining that though I was bit confused but now
that I know whats going on it makes sense.
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"Junk Mail" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>The strange this is that it compiles with out any errors...why?

Because it matches the first overloaded function. AnsiString has a a
constructor which accepts an 'int', and the C++ language can convert an
'int' to a 'bool'.

>Second strange thing is that when I step through this code it is
actually
>invokeing the 1st function which has paramters of int, AnsiString, bool.

Correct. That is the only overloaded function you have which can accept
just 3 parameters, and the values are are supplying to it can be
implicitally converted to the types that the function is expecting.

>I'm wondering why the compiler didn't catch this.

It is not an error. It is doing eactly what it is supposed to be doing.


Gambit