질문은 코드입니다. 두 번째 기능이 첫 번째 기능보다 더 특수한 것처럼 보입니다. 왜 더 일반적인 코드가 다음 코드에서 호출됩니까? 다른 기능을 사용하려면 어떻게해야합니까?함수 템플릿의 과부하 해결
template <typename T>
class Base{
public:
Base(){}
void print() const {cout<<"Base class"<<endl;}
};
template <typename T>
class Derived :public Base<T>{
public:
Derived() {}
void print() const {cout<<"Derived class"<<endl;}
};
template <typename T>
void func(T x){ // <----- Why is function is called?
x.print();
cout<<"in func(T)"<<endl;
}
template <typename T>
void func(const Base<T>& x){
x.print();
cout<<"in func(Base<T>)"<<endl;
}
int main() {
Base<int> b;
Derived<int> d;
func(d);
return 0;
}
파생 된 개체를 함수에 전달 중입니다.
이 게시물은 당신을 도울 수 있습니다 : http://stackoverflow.com/questions/22411482/c-template-functions-overload-resolution. @NikosAthanasiou의 답변보기. – chema989
가능한 복제본 : http://stackoverflow.com/questions/31563580/c-templated-function-overloading-rules?lq=1 – sameerkn