avatar
mw
1
Now i am quite confused about template
we are supposed to use it for some sort of paramiterized stuff,
template f(T)
however, if more than one classes can be fed to a certain function, they must
share certain similar properties and thus can be modeled as a hierarchy. Then
we can just define the function as f(class A) where A is the base.
Then , is template only useful in containers?
can anyone give me some corrections?
thanks lot!
avatar
a*a
2
You never heard of specialization? suggest you study the parameter
matching order of templated functions.

【在 mw 的大作中提到】
: Now i am quite confused about template
: we are supposed to use it for some sort of paramiterized stuff,
: template f(T)
: however, if more than one classes can be fed to a certain function, they must
: share certain similar properties and thus can be modeled as a hierarchy. Then
: we can just define the function as f(class A) where A is the base.
: Then , is template only useful in containers?
: can anyone give me some corrections?
: thanks lot!

avatar
g*g
3
It's good to have compiler check as much as possible and as early as
possible. At the same time, we want to have flexibility.
template will make perfect sense in Collection classes, for example,
Vector is way better than Vector where you will convert
every object to int later on.

【在 mw 的大作中提到】
: Now i am quite confused about template
: we are supposed to use it for some sort of paramiterized stuff,
: template f(T)
: however, if more than one classes can be fed to a certain function, they must
: share certain similar properties and thus can be modeled as a hierarchy. Then
: we can just define the function as f(class A) where A is the base.
: Then , is template only useful in containers?
: can anyone give me some corrections?
: thanks lot!

avatar
a*n
4
I think you can understand template as another kind of polymorphism.
Usually polymorphism refers to inheritance and virtual functions.

【在 mw 的大作中提到】
: Now i am quite confused about template
: we are supposed to use it for some sort of paramiterized stuff,
: template f(T)
: however, if more than one classes can be fed to a certain function, they must
: share certain similar properties and thus can be modeled as a hierarchy. Then
: we can just define the function as f(class A) where A is the base.
: Then , is template only useful in containers?
: can anyone give me some corrections?
: thanks lot!

avatar
X*r
5
Template in C++ is a (rough) realization of generic programming,
which is totally a different approach against object-oriented programming.
Also, as a side effect, template allows (limited) compile-time computation.
Nevertheless, your question is a good one. Whether it's better to use
templates or polymorphism for a function or an algorithm really depends
on individual situation.
For example, let's say your function is to quick-sort. The most basic
implementation only requires the elements being

【在 mw 的大作中提到】
: Now i am quite confused about template
: we are supposed to use it for some sort of paramiterized stuff,
: template f(T)
: however, if more than one classes can be fed to a certain function, they must
: share certain similar properties and thus can be modeled as a hierarchy. Then
: we can just define the function as f(class A) where A is the base.
: Then , is template only useful in containers?
: can anyone give me some corrections?
: thanks lot!

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。