I have the following piece of code:
class ICookable
{
public:
virtual void CookMe () = 0;
virtual ~ICookable () {};
};
class Egg : public ICookable
{
public:
virtual void CookMe () {cout << "Egg cooked!" << endl;}
};
template <class T>
void Cook (T&)
{
cout << "Item Uncookable!" << endl;
}
template <>
void Cook (ICookable& c)
{
c.CookMe ();
}
int _tmain(int argc, _TCHAR* argv[])
{
Egg egg;
Cook (egg);
return 0;
}
I want the Cook function to behave differently depending on whether its parameter is inherited from ICookable interface or not. However in the example above I get "Item Uncookable" message for Egg parameter, unless I manually write:
Cook<ICookable> (egg);
Is there a way to let the compiler automatically choose the right realization for ICookable descendants?