#include "head.h" template<typename T> class Beta { private: template<typename V> class hold { private: V val; public: hold(V v):val(v){} void show() const {cout<<val<<endl;} V value() const {return val;} } hold<T> q; hold<int> n; public: Beta(T t,int i):q(t),n(i){} template<typename U> U blab(U a,T b) { return (((q.value+n.value())*a)/t); } void show() const { q.show(); n.show(); } }; int main() { Beta<double> t(3.5,3); t.show(); cout<<t.blab(10,2.3)<<endl; cout<<t.blab(10.0,2.3)<<endl; cout<<"Done"<<endl; return 0; }
两种方法:一种是class里面声明且定义。一种是class里面declaration,但是在类外面definition
注意class外面definition的时候,由于template是nested(潜入)的,因此须使用句法:
[cpp]
view plain
copy
template<typename T> template<typename U>
而不是
template<typename T,typename V>
定义还须之处hold和blab是Beta<T>class的成员,通过使用作用域解析操作符。
转载请注明原文地址: https://ju.6miu.com/read-6980.html