 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Dennis Jones Guest
|
Posted: Wed Jun 08, 2005 4:23 am Post subject: incorrect overloaded function called? |
|
|
Hi,
I am seeing some strange behavior in an overloaded function call. I have
something like this:
void func( TDateTime t )
{
}
void func( int i )
{
}
Now, suppose I call func() like this:
func( TDateTime( 0, 0, 0, 0 ) );
But when I do this, "func( int )" gets invoked!!
But if I do this instead:
TDateTime t( 0, 0, 0, 0 );
func( t );
....then "func( TDateTime )" gets invoked as expected. Now, I know TDateTime
has an int() operator, but shouldn't the type system see that func(
TDateTime ) is a better match than func( int ) for the first case?
- Dennis
|
|
| Back to top |
|
 |
Remy Lebeau (TeamB) Guest
|
Posted: Wed Jun 08, 2005 5:46 am Post subject: Re: incorrect overloaded function called? |
|
|
"Dennis Jones" <nospam (AT) nospam (DOT) com> wrote
| Quote: | void func( TDateTime t )
|
You should pass your object by reference:
void func(const TDateTime &t)
Gambit
|
|
| Back to top |
|
 |
Dennis Jones Guest
|
Posted: Wed Jun 08, 2005 6:22 am Post subject: Re: incorrect overloaded function called? |
|
|
"Remy Lebeau (TeamB)" <no.spam (AT) no (DOT) spam.com> wrote
| Quote: |
"Dennis Jones" <nospam (AT) nospam (DOT) com> wrote in message
news:42a672c5$1 (AT) newsgroups (DOT) borland.com...
void func( TDateTime t )
You should pass your object by reference:
void func(const TDateTime &t)
|
Actually, I do, and that is the problem. However, I don't have a 'const'
modifier on it, and that also appears to be a key element in the problem.
For instance, the following prototype does not match (which is what I had):
void func( TDateTime &t )
....while both of these do:
void func( TDateTime t )
void func( const TDateTime &t )
And now that I think about it, I have a vague understanding of why this is:
the object being passed is a temporary, so it can only be matched by a const
reference or a non-reference (because temporaries cannot be changed).
Otherwise the other function (with an int argument), although it requires an
implicit type conversion, is a better match because it is not a reference.
Thanks for the bump in the right direction.
- Dennis
|
|
| Back to top |
|
 |
Alan Bellingham Guest
|
Posted: Wed Jun 08, 2005 8:15 am Post subject: Re: incorrect overloaded function called? |
|
|
"Dennis Jones" <nospam (AT) nospam (DOT) com> wrote:
| Quote: | the object being passed is a temporary, so it can only be matched by a const
reference or a non-reference (because temporaries cannot be changed).
Otherwise the other function (with an int argument), although it requires an
implicit type conversion, is a better match because it is not a reference.
|
Absolutely correct.
Alan Bellingham
--
Me <url:mailto:alanb (AT) episys (DOT) com> <url:http://www.doughnut.demon.co.uk/>
ACCU - C, C++ and Java programming <url:http://accu.org/>
The 2004 Discworld Convention <url:http://dwcon.org/>
|
|
| Back to top |
|
 |
Dennis Jones Guest
|
Posted: Wed Jun 08, 2005 3:59 pm Post subject: Re: incorrect overloaded function called? |
|
|
"Alan Bellingham" <alanb (AT) episys (DOT) com> wrote
| Quote: | "Dennis Jones" <nospam (AT) nospam (DOT) com> wrote:
the object being passed is a temporary, so it can only be matched by a
const
reference or a non-reference (because temporaries cannot be changed).
Otherwise the other function (with an int argument), although it requires
an
implicit type conversion, is a better match because it is not a
reference.
Absolutely correct.
|
Thanks for the confirmation, Alan!
- Dennis
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|