由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - why use template?
相关主题
大家讨论一下程序员的三观是那三观?Can you offer some performance/scalability programming tips (for interview)
template questionhow to use stack(s) to realize a queque
How can Matlan realize such function as C++厚着脸皮问个很简单的问题
Node之父改行golang了[合集] Help: realize a simple function in HTML
Is this correct?以前那个悬赏问 68K asm 的删贴啦?
What does int & mean? C/C++ question有没有可能用big data分析房价走势
I didn't realize thatis Haskell good?
find start point of loop from linked list两道Java面试问题
相关话题的讨论汇总
话题: template话题: function话题: use话题: certain
进入Programming版参与讨论
1 (共1页)
mw
发帖数: 525
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!
a**a
发帖数: 416
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!

g*****g
发帖数: 34805
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!

a**n
发帖数: 313
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!

X****r
发帖数: 3557
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!

1 (共1页)
进入Programming版参与讨论
相关主题
两道Java面试问题Is this correct?
问个先后的问题What does int & mean? C/C++ question
高人帮我看看一个简单的script,为什么nohup有问题? (转载)I didn't realize that
C: question on getenvfind start point of loop from linked list
大家讨论一下程序员的三观是那三观?Can you offer some performance/scalability programming tips (for interview)
template questionhow to use stack(s) to realize a queque
How can Matlan realize such function as C++厚着脸皮问个很简单的问题
Node之父改行golang了[合集] Help: realize a simple function in HTML
相关话题的讨论汇总
话题: template话题: function话题: use话题: certain