C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

function overloading with default argument

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
praveen
Guest





PostPosted: Sat Jan 15, 2005 10:26 am    Post subject: function overloading with default argument Reply with 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! ]
Back to top
Francis Glassborow
Guest





PostPosted: Sun Jan 16, 2005 4:13 am    Post subject: Re: function overloading with default argument Reply with quote



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





PostPosted: Sun Jan 16, 2005 4:28 am    Post subject: Re: function overloading with default argument Reply with quote



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





PostPosted: Sun Jan 16, 2005 4:39 am    Post subject: Re: function overloading with default argument Reply with quote


"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" Smile ]

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

Quote:


[ 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
Sumit Rajan
Guest





PostPosted: Sun Jan 16, 2005 4:40 am    Post subject: Re: function overloading with default argument Reply with quote

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





PostPosted: Sun Jan 16, 2005 4:46 am    Post subject: Re: function overloading with default argument Reply with quote

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





PostPosted: Sun Jan 16, 2005 4:55 am    Post subject: Re: function overloading with default argument Reply with quote

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





PostPosted: Sun Jan 16, 2005 4:57 am    Post subject: Re: function overloading with default argument Reply with quote

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





PostPosted: Sun Jan 16, 2005 5:00 am    Post subject: Re: function overloading with default argument Reply with quote

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





PostPosted: Sun Jan 16, 2005 5:04 am    Post subject: Re: function overloading with default argument Reply with quote

"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





PostPosted: Sun Jan 16, 2005 5:23 am    Post subject: Re: function overloading with default argument Reply with quote

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





PostPosted: Sun Jan 16, 2005 5:23 am    Post subject: Re: function overloading with default argument Reply with quote


"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
Quote:


[ 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
L.Suresh
Guest





PostPosted: Sun Jan 16, 2005 5:25 am    Post subject: Re: function overloading with default argument Reply with quote

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





PostPosted: Sun Jan 16, 2005 5:29 am    Post subject: Re: function overloading with default argument Reply with quote

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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.