 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
praveen Guest
|
Posted: Sat Jan 15, 2005 10:26 am Post subject: function overloading with default argument |
|
|
I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Francis Glassborow Guest
|
Posted: Sun Jan 16, 2005 4:13 am Post subject: Re: function overloading with default argument |
|
|
In article <1105774712.050668.268380 (AT) z14g2000cwz (DOT) googlegroups.com>,
praveen <praveen.varrier (AT) gmail (DOT) com> writes
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
|
I am assuming that was a typo and you meant:
fun1(int a, int b = 2);
In addition neither of the above declarations would be accepted by a
conforming compiler because they lack a return type.
| Quote: |
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
Neither. The compiler will issue an ambiguity error. It will not be
possible to call your first version in any context where the second one
is visible.
As a matter of good design, overloaded functions should 'do the same
thing'. Given that, replace your overload set with:
int fun1(int, int);
inline int fun1(int a){ return fun1(a, 2); }
--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
carletoncourses@yahoo.ca Guest
|
Posted: Sun Jan 16, 2005 4:28 am Post subject: Re: function overloading with default argument |
|
|
Hello Praveen:
The compiler should generate an error in this case.
Say I have the program
class A
{
public:
void f1( int a ) { }
void f1( int a, int b = 2) { }
};
int main(int argc, char * argv[] )
{
A a;
a.f1(3);
return 0;
}
If I try to compile it using VC++ 6.0, I would get an error
like this:
error C2668: 'f1' : ambiguous call to overloaded function
Bill
praveen wrote:
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Abhishek Pandey Guest
|
Posted: Sun Jan 16, 2005 4:39 am Post subject: Re: function overloading with default argument |
|
|
"praveen" <praveen.varrier (AT) gmail (DOT) com> wrote
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
None. Have you tried to run this code yourself.
The call is very clearly ambiguous [If I assume you meant "int a" in the
second declaration instead of "in a" ]
Read Scott Meyers' Effective C++ : Item 24, Choose carefully between
function overloading and parameter defaulting
to understand when to use default argument and when to use function
overloading.
Thanks
Abhishek
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Sumit Rajan Guest
|
Posted: Sun Jan 16, 2005 4:40 am Post subject: Re: function overloading with default argument |
|
|
praveen wrote:
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
Compile-time error.
--
Sumit Rajan <sumit.rajan (AT) gmail (DOT) com>
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
carletoncourses@yahoo.ca Guest
|
Posted: Sun Jan 16, 2005 4:46 am Post subject: Re: function overloading with default argument |
|
|
Hello:
Compiler would treat the calls to such function as
ambiguous. Lets see an example program to explain:
class A
{
public:
void fun1( int a ) { }
void fun1( int a, int b = 2) { }
};
int main(int argc, char * argv[] )
{
A a;
a.fun1(3);
return 0;
}
If I try compiling it using Visual C++ 6.0, I get
this error:
error C2668: 'fun1' : ambiguous call to overloaded function
Hope that answers your question.
Bill
praveen wrote:
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
René Kjellerup Guest
|
Posted: Sun Jan 16, 2005 4:55 am Post subject: Re: function overloading with default argument |
|
|
it will never compile
as the declaration is ambiguous
so the answer would be none of them.
René
praveen wrote:
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 265.6.12 - Release Date: 14-01-2005
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jesper Schmidt Guest
|
Posted: Sun Jan 16, 2005 4:57 am Post subject: Re: function overloading with default argument |
|
|
The code shouldn't compile because both overloads match the call equally
well (ambiguous call to overloaded function).
-Jesper
"praveen" <praveen.varrier (AT) gmail (DOT) com> wrote
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Kanze Guest
|
Posted: Sun Jan 16, 2005 5:00 am Post subject: Re: function overloading with default argument |
|
|
praveen wrote:
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
|
Which is, I believe, illegal.
| Quote: | When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
Neither. The call is ambiguous, and the code won't compile.
For overloading purposes, a function with a default parameter is
considered as if it were two functions.
--
James Kanze home: www.gabi-soft.fr
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 pl. Pierre Sémard, 78210 St.-Cyr-l'École, France +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Sun Jan 16, 2005 5:04 am Post subject: Re: function overloading with default argument |
|
|
"praveen" <praveen.varrier (AT) gmail (DOT) com> wrote...
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
Ambiguous.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Fah!m Guest
|
Posted: Sun Jan 16, 2005 5:23 am Post subject: Re: function overloading with default argument |
|
|
I think that depending on the compiler u r using...u shud get an error
as there is ambiguity in the above case...compiler shud complain...
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jeffrey Baker Guest
|
Posted: Sun Jan 16, 2005 5:23 am Post subject: Re: function overloading with default argument |
|
|
"praveen" <praveen.varrier (AT) gmail (DOT) com> wrote
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
You will get an error message :fun1: ambiguous call to overloaded function
The default argument won't allow an overload. The two argument needs only
one argument, but the compiler sees two, so it won't accept it.
jb
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
L.Suresh Guest
|
Posted: Sun Jan 16, 2005 5:25 am Post subject: Re: function overloading with default argument |
|
|
There are two candidate functions. The call is ambiguous.
--lsu
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Maciej Sobczak Guest
|
Posted: Sun Jan 16, 2005 5:29 am Post subject: Re: function overloading with default argument |
|
|
praveen wrote:
| Quote: | I am in a big dilema now
the problem is,I have an overloaded function,
1) fun1(int a);
2) fun1(in a,int b=2);
When i call it this way,
fun1(3);
which overloaded fun1(..) will be called
|
This call is ambiguous.
For the purpose of overloading resolution, the parameters of candidate
functions with additional default values are "truncated".
Above, there are two candidate functions. First is:
fun1(int a) // 1.
the other is:
fun1(int a, int b = 2) // 2.
but the additional default parameter is truncated, giving:
fun1(int a) // 2.
Since, after truncation, 1. and 2. are the same (precisely: none is
better than the other), the call is ambiguous.
See 13.1/3 and 13.3.2/2 for details.
--
Maciej Sobczak : http://www.msobczak.com/
Programming : http://www.msobczak.com/prog/
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| 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
|
|