Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
4.75/5 (4 votes)
See more:
Please see this code

C#
#include "stdafx.h"
#include "iostream"

void fun(int i)
{
    std::cout<<"Int Overload called";
}

void fun(int *i)
{
    std::cout<<"Pointer Overload called";
}

int main ()
{

    fun(0); \\gets called successfully 

  return 0;
}


Output : Int Overload called

My question is why call to fun does not leads to ambiguity, how compiler calls 'int' implementation and not pointer's implementation.
Posted

Nice question!

Found an answer at http://www.learncpp.com/cpp-tutorial/76-function-overloading/[^].
Basically, int is considered (by your compiler) a better match than int * for 0.
You may also like this: http://xania.org/200711/ambiguous-overloading[^]

Hope this helps,
Pablo.
 
Share this answer
 
Comments
[no name] 9-Apr-12 7:57am    
Thanks. 5!
Compiler calls int implementation because int* type is unsigned int not just int

Now try changing

C++
void fun(int i)
{
    std::cout<<"Int Overload called";
}

to
C++
void fun(unsigned int i)
{
    std::cout<<"Int Overload called";
}


The compiler cribs and says
'error C2668: 'fun' : ambiguous call to overloaded function'


Good question
 
Share this answer
 
Comments
[no name] 9-Apr-12 7:57am    
Hey nice explanation. Thanks.5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900