为什么它不给歧义带来错误? [英] Why it is does not give error about ambiguity?

查看:107
本文介绍了为什么它不给歧义带来错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参见此代码

Please see this code

#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;
}



输出:称为Int重载

我的问题是为什么调用fun不会导致歧义,编译器如何调用"int"实现而不是指针的实现.



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.

推荐答案

好问题!

http://www.learncpp.com/cpp-tutorial/76-function-overloading找到了答案/ [^ ] .
基本上,(编译器)认为intint * int *更匹配.
您可能还会喜欢: http://xania.org/200711/ambiguous-overloading [
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.


编译器调用int实现,因为int *类型是无符号的int不仅仅是int

现在尝试更改

Compiler calls int implementation because int* type is unsigned int not just int

Now try changing

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




to

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



编译器摇摇晃晃地说
``错误C2668:``乐趣'':对重载函数的模棱两可的调用''


好问题



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


Good question


这篇关于为什么它不给歧义带来错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆