에 과부하 해결 방법이기능 const를
#include <iostream>
class A
{
public:
void fun(int x) const
{
std::cout<<"int x"<<std::endl;
}
void fun(const int x)
{
std::cout<<"const int x"<<std::endl;
}
void fun(int &x)
{
std::cout<<"int &x"<<std::endl;
}
void fun(const int &x)
{
std::cout<<"const int &x"<<std::endl;
}
};
int main()
{
A obj;
int a = 10;
const int b = 10;
int& ref = a;
const int& ref1 = b;
obj.fun(a);
obj.fun(b);
obj.fun(ref);
obj.fun(ref1);
return 0;
}
이이 모호성을 얻을 컴파일 고려하지만 그들 중 누구도 재미 (CONST의 INT의 x)를하지만,이 코드를하게 제거가 제대로
- 를 컴파일하기에 그것으로 인해 말한다
과부하 해결 중 const const fun (const int & x) 및 함수 자체를 const로 변경하면 어떤 차이가 발생합니까?
다양한 조합을 시도 좀 더 의심이있다, 그래서 과부하 해상도,
이게 당신이 묻는 것입니까? http://stackoverflow.com/q/3682049/2065121 –
감사합니다 그것은 첫 번째 지점에 대한 답변입니다. 그러나 일반적인 규칙이 있습니까? 우리가 fun (int x) const에서 const를 제거하면 예를 들어 fun (const int &)와 모호하다 그렇지 않으면 –
[관련 게시물 from yesterday] (http://stackoverflow.com/questions/17033074/overloading-on-const- 왜 최상위 'const'한정자에 관한 - 참조에 의한 작업 - 왜 - 할 - 왜 - 할 -). – juanchopanza