473,698 Members | 2,334 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ambiguity related to overloaded functions

Consider the program

#include <iostream>

using namespace std;

void fn(const char * str, char x = 'Y')
{
cout << "from fn(const char *, char) - " << str << endl;
return;
}

void fn(const char * str)
{
cout << "from fn(const char *) - " << str << endl;
return;
}

int main( )
{
// fn("test string");
return 0;
}

This program compiles without any error or warning. If I remove the
comment in the statement
// fn("test string");
in main( ), I am getting ambiguity error. Why doesn't the compiler
detect the ambiguity even when the call to fn( ) was not made ? What
is the expected behaviour ? Where am I going wrong ?

Kindly explain.

Thanks
V.Subramanian

Jun 25 '07 #1
2 1725
On Mon, 25 Jun 2007 00:32:04 -0700, "su************ **@yahoo.com,
India" <su************ **@yahoo.comwro te:
>Consider the program

#include <iostream>

using namespace std;

void fn(const char * str, char x = 'Y')
{
cout << "from fn(const char *, char) - " << str << endl;
return;
}

void fn(const char * str)
{
cout << "from fn(const char *) - " << str << endl;
return;
}

int main( )
{
// fn("test string");
return 0;
}

This program compiles without any error or warning. If I remove the
comment in the statement
// fn("test string");
in main( ), I am getting ambiguity error. Why doesn't the compiler
detect the ambiguity even when the call to fn( ) was not made ? What
is the expected behaviour ? Where am I going wrong ?
Ambiguity condition is tested only when the function is needed, that
is, when compiler tries to find an adequate funtiona for the call
If commented line were:
fn("test string",'N');
there would be no ambiguity at all.

Best regards,

Zara
Jun 25 '07 #2
su************* *@yahoo.com, India wrote:
Consider the program

#include <iostream>

using namespace std;

void fn(const char * str, char x = 'Y')
{
cout << "from fn(const char *, char) - " << str << endl;
return;
}

void fn(const char * str)
{
cout << "from fn(const char *) - " << str << endl;
return;
}

int main( )
{
// fn("test string");
return 0;
}

This program compiles without any error or warning. If I remove the
comment in the statement
// fn("test string");
in main( ), I am getting ambiguity error. Why doesn't the compiler
detect the ambiguity even when the call to fn( ) was not made ? What
is the expected behaviour ? Where am I going wrong ?

Kindly explain.

Thanks
V.Subramanian
Because functions themselves are not ambiguous. Calls to functions can
be. Even in this program it would be possible to call the second
version of fn unambiguously. One method of doing so might be:
void (*p)(const char * str) = fn ;
p("test string") ;
--
Alan Johnson
Jun 25 '07 #3

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

Similar topics

8
17872
by: Nitin Bhardwaj | last post by:
Thanx in advance for the response... I wanna enquire ( as it is asked many a times in Interviews that i face as an Engg PostGraduate ) about the overloading capability of the C++ Language. Why can't the = (assignment) operator be overloaded as a friend function ? I work in VS 6.0 ( Win2000 ) as when i referred the MSDN documen'n it said the following :
5
13152
by: IvD² | last post by:
During a project I ran into trouble when using multiple inheritance. I was able to resolve the problem, but was unable to really understand the reasons for the error. Here is a short example of the problem: class BaseA { ...
44
2403
by: bahadir.balban | last post by:
Hi, What's the best way to implement an overloaded function in C? For instance if you want to have generic print function for various structures, my implementation would be with a case statement: void print_structs(void * struct_argument, enum struct_type stype) { switch(stype) { case STRUCT_TYPE_1:
3
2247
by: cybertof | last post by:
Hello, Is it possible in C# to have 2 overloaded functions with - same names - same parameters - different return type values If no, is it possible in another language ?
2
3881
by: desktop | last post by:
If a function should work with different types you normally overload it: void myfun(int a){ // do int stuff } void myfun(std::string str){ // do string stuff }
54
3920
by: Rasjid | last post by:
Hello, I have just joined and this is my first post. I have never been able to resolve the issue of order of evaluation in C/C++ and the related issue of precedence of operators, use of parentheses. 1) "The order of evaluation of subexpressions is determined by the precedence and grouping of operators."
3
2141
by: Adam Nielsen | last post by:
Hi everyone, I've run into yet another quirk with templates, which IMHO is a somewhat limiting feature of the language. It seems that if you inherit multiple classes, and those classes have two functions with the same name, this is an error - even when the functions take different parameters, and they would happily exist as overloaded functions if they were declared in the same class instead of being inherited.
4
3374
by: abendstund | last post by:
Hi, I have the following code and trouble with ambiguity due to operator overloading.. The code is also at http://paste.nn-d.de/441 snip>>
0
8603
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8861
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3046
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2001
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.