Question is the code. It looks like the 2nd function is more special than the 1st one. Why the more general one is called in the following code? How can I make the other function to be used?
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;
}
Note that I am passing the Derived object to the function.