#include <iostream> using namespace std; template<typename T> class Phone { public: void show() { T* pThis = static_cast<T*>(this); pThis->printName(); } public: void printName(){ cout<<"Phone ... "<<endl; } }; class Nokia : public Phone<Nokia> { public: void printName(){ cout<<"Nokia Phone ... "<<endl; } }; class Samang:public Phone<Samang> { public: void printName(){ cout<<"Samang Phone ... "<<endl; } }; class UnknowPhone:public Phone<UnknowPhone> { }; int main() { Nokia nokia; Samang samang; UnknowPhone unPhone; nokia.show(); samang.show(); unPhone.show(); }
输出结果如下:
Nokia Phone ...
Samang Phone ...
Phone ...
请按任意键继续. . .