1234567891011121314151617181920212223242526272829
#include <iostream> #include<typeinfo> using namespace std; template<class T> class num { protected: T val; public: num(T x):val(x){} virtual T getval() {return val;} }; template<class T> class sqrnum:public num<T> { public: sqrnum(T x):num<T>(x){} T getval(){return val*val;} }; int main() { num<int> *bp, num_int(2); sqrnum<int> *dp, sqr_int(3); num<double> *bpd,num_double(1.2); bp=dynamic_cast<num<int>*>(&sqr_int); cout<<typeid(*bp).name()<<endl; return 0; }
12
// T getval(){return val*val;} T getval() { return num<T>::val * this->val ; }