模板功能和dynamic_cast [英] Template function and dynamic_cast
问题描述
请看一下这种方法:
template< class C> void f(C * ptrAny){
Fruit * ptrFruit = dynamic_cast< Fruit *>(ptrAny);
if(ptrFruit){
//做一些特定于水果的事情
}
//继续使用ptrAny它是什么
}
>
所以我有一个模板功能,但对于水果我也想做某个特定的东西。
一切都很好,直到我碰巧用非多态的
类型调用F.然后barfs出现编译错误。
那么如果模板化C类型可以是多态的还是
那么我怎么能这样做呢?注意我不想用这样的非模板重载f:
void f(Fruit * ptrFruit){...}
....因为我有几个模板化的函数,比如f,还因为
编译器如何知道哪一个调用Fruit,
Fruit特定的一个或模板化的一个实例化为水果?
感谢您的帮助。
Please take a look at this method:
template<class C> void f(C* ptrAny) {
Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny);
if(ptrFruit) {
// do something specific to fruits
}
// Carry on using ptrAny whatever it is
}
So I have a template function, but for Fruits I also want to do a
certain specific thing.
All is well and good until I happen to call F with a non-polymorphic
type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or
not? NB I don''t want to overload f with a non-template like this:
void f(Fruit* ptrFruit) {...}
....because I have several templated functions like f, and also because
how would the compiler know which one to call for a Fruit, the
Fruit-specific one or the templated one instantiated for a Fruit?
Thanks for any help.
推荐答案
>
ri***********@yahoo.co.uk 写道:
请看一下这个方法:
template< class C> void f(C * ptrAny){
Fruit * ptrFruit = dynamic_cast< Fruit *>(ptrAny);
if(ptrFruit){
//做一些特定的水果
//
//继续使用ptrAny它是什么
}
所以我有一个模板功能,但对于Fruits我也想做一些具体的事情。
一切都很好,直到我碰巧用非多态的类型调用F.然后barfs出现编译错误。
那么我怎么能这样做,模板化的C类型可以是多态的还是不是? NB我不想用这样的非模板超载f:
void f(Fruit * ptrFruit){...}
我想你有没有选择。 "专业化"是游戏的名称。
...因为我有几个模板化的函数,比如f
那么什么?
,还因为
如何编译器会知道哪一个调用Fruit,
Fruit特定的一个还是模板化的Fruit实例化?
编译器足够聪明,可以选择专门的功能
模板化的
感谢您的帮助。
Please take a look at this method:
template<class C> void f(C* ptrAny) {
Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny);
if(ptrFruit) {
// do something specific to fruits
}
// Carry on using ptrAny whatever it is
}
So I have a template function, but for Fruits I also want to do a
certain specific thing.
All is well and good until I happen to call F with a non-polymorphic
type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or
not? NB I don''t want to overload f with a non-template like this:
void f(Fruit* ptrFruit) {...}
I think you have no choice. "Specialization" is the name of the game.
...because I have several templated functions like f so what?
, and also because
how would the compiler know which one to call for a Fruit, the
Fruit-specific one or the templated one instantiated for a Fruit?
the compiler is smart enough to pick the specialized function instead
of the templated one
Thanks for any help.
请参阅以下代码:
class Fruit
{
public:
void IsRipe(){}
虚拟〜水果(){}
};
class NonF
{
};
模板< class C>
void f_impl(C * ptrAny)
{
//做常见的东西
}
模板< class C>
void f(C * ptrAny)
{
//做常用处理
f_impl(ptrAny);
}
模板<>
void f(Fruit * ptrFruit)>
{
//为水果做特定的事情
ptrFruit-> IsRipe();
/ /然后,做共同的处理
f_impl(ptrFruit);
}
int main()
{
水果fr;
NonF nf;
f(& fr);
f(& nf);
返回0;
}
/ dan
see the code below:
class Fruit
{
public:
void IsRipe(){}
virtual ~Fruit(){}
};
class NonF
{
};
template <class C>
void f_impl(C* ptrAny)
{
// do the common stuff
}
template<class C>
void f(C* ptrAny)
{
// do the common processing
f_impl(ptrAny);
}
template<>
void f(Fruit* ptrFruit)
{
// do something specific for fruit
ptrFruit->IsRipe();
// then, do the common processing
f_impl(ptrFruit);
}
int main()
{
Fruit fr;
NonF nf;
f(&fr);
f(&nf);
return 0;
}
/dan
ri***********@yahoo.co.uk sade:
void f(Fruit * ptrFruit){...}
...因为我有几个像f一样的模板化函数,还因为
如何编译器会知道哪一个调用Fruit,
Fruit特定的一个还是模板化的一个Fruit实现?
void f(Fruit* ptrFruit) {...}
...because I have several templated functions like f, and also because
how would the compiler know which one to call for a Fruit, the
Fruit-specific one or the templated one instantiated for a Fruit?
那么也许是一个模板专业化,没有编译器混淆。
模板< typename T> void f(T *){}
template<> void f(Fruit *){}
Tobias
-
重要提示:此电子邮件的内容和附件是保密的
并且可能受到法律特权和/或受版权保护。
禁止将其任何部分复制或传播给他人,并且可能
是非法的。
Then perhaps a template specialization, no compiler confusion there.
template<typename T> void f(T *) {}
template<> void f(Fruit *) {}
Tobias
--
IMPORTANT: The contents of this email and attachments are confidential
and may be subject to legal privilege and/or protected by copyright.
Copying or communicating any part of it to others is prohibited and may
be unlawful.
< ri *********** @ yahoo.co。 UK>在消息中写道
news:11 ********************** @ g44g2000cwa.googlegr oups.com ...
<ri***********@yahoo.co.uk> wrote in message
news:11**********************@g44g2000cwa.googlegr oups.com...
请看一下这个方法:
template< class C> void f(C * ptrAny){
Fruit * ptrFruit = dynamic_cast< Fruit *>(ptrAny);
if(ptrFruit){
//做一些特定的水果
//
//继续使用ptrAny它是什么
}
所以我有一个模板功能,但对于Fruits我也想做一些具体的事情。
一切都很好,直到我碰巧用非多态的类型调用F.然后barfs出现编译错误。
那么我怎么能这样做,模板化的C类型可以是多态的还是不是?注意我不想用这样的非模板重载f:
void f(Fruit * ptrFruit){...}
...因为我有几个模板化的函数,比如f,并且还因为编译器如何知道哪一个要调用Fruit,
Fruit特定的一个还是模板化的Fruit实现?
感谢您的帮助。
Please take a look at this method:
template<class C> void f(C* ptrAny) {
Fruit* ptrFruit = dynamic_cast<Fruit*>(ptrAny);
if(ptrFruit) {
// do something specific to fruits
}
// Carry on using ptrAny whatever it is
}
So I have a template function, but for Fruits I also want to do a
certain specific thing.
All is well and good until I happen to call F with a non-polymorphic
type. Then it barfs with a compile error.
So how can I do this where the templated C type can be polymorphic or
not? NB I don''t want to overload f with a non-template like this:
void f(Fruit* ptrFruit) {...}
...because I have several templated functions like f, and also because
how would the compiler know which one to call for a Fruit, the
Fruit-specific one or the templated one instantiated for a Fruit?
Thanks for any help.
你为什么不这样做:
void f(Fruit * ptrFruit)
{
FruitSpecific();
f< Fruit>();
}
模板< typename T>
void f(T * ptrAny)
{
//继续
}
Ben
Why don''t you do:
void f(Fruit* ptrFruit)
{
FruitSpecific();
f<Fruit>();
}
template <typename T>
void f(T* ptrAny)
{
// carry on
}
Ben
这篇关于模板功能和dynamic_cast的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!