为什么对fun()函数的调用不明确? [重复]

问题描述 投票:1回答:1

此问题已经在这里有了答案:

为什么t1.fun()可以完美地调用void fun(),但没有调用void fun() const,对fun()的调用不应该模棱两可,因为void fun() const也可以由非常量对象t1调用?

#include<iostream> 
using namespace std; 

class Test 
{ 
public: 
    Test (int i) { } 
    void fun() const
    { 
        cout << "fun() const called " << endl; 
    } 
    void fun() 
    { 
        cout << "fun() called " << endl; 
    } 
}; 

int main() 
{ 
    Test t1 (10); 
    t1.fun();
    return 0; 
} 
c++ overloading const
1个回答
1
投票

因为void fun() const也可以被非常量对象t1调用]]

是,可以在非常量对象上调用它,但是它需要在要调用的对象上从非常量到const进行隐式转换(资格转换),然后非常量版本会在重载中获胜分辨率。

© www.soinside.com 2019 - 2024. All rights reserved.