templates - C++ parameterized constructor In a templated class -
i start learning templates in c++ , not sure if need include template <class t>
for implementation of parameterized constructor.
template <class t> class { t num; //default constructor (parameterized) template <class t>//getting error a(t value) { num=value;} }
i error shadow template parm < class t > when include template<class t>
constructor.but works when comment out.
i wondering why dont need declare template constructor.
if you're sure need templated constructor function, use different parameter name template:
template <class t> class { t num; //default constructor (parameterized) template <class u> // ^ a(u value) // ^ { num=value;} };
otherwise template parameter name t
used templated constructor function shadow name used in class template declaration.
as asking in comment "what occasions use templated constructor?"
it's cases like
a<double> a(0.1f);
note above simple example, , wouldn't need templated constructor. it's demonstrate templated constructor used conversion types different type used in instantiation.
"i wondering why dont need declare template constructor."
a template class without (or additional) non-templated constructor use t
specified class template parameter parameter type
template <class t> class { t num; a(t value) // ^ { num=value;} };
this standard case, template classes don't need templated constructor or other templated functions.
Comments
Post a Comment