你怎么知道对象foo是否是CBar类? [英] How do you find out if object foo is class CBar?
问题描述
我有一个基础对象,CBase和2个子对象CSub1和CSub2。
鉴于我有一个指向CBase的指针,如果对象是CSub1或CSub2,我可以找到(很容易以便携式
的方式)。 (或两者都没有,但在我的应用程序中总是
1或其他)
在Delphi中,您将使用IS操作,例如
如果myObject是CSub1那么
.....
有人可以告诉我C ++ equivelent吗?
谢谢
乔治
Hi,
I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.
Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its always
1 or the other)
In Delp you would use the IS operation, eg
if myObject IS CSub1 then
.....
Can someone tell me the C++ equivelent please?
thanks
George
推荐答案
George Styles写道:
George Styles wrote:
我有一个基础对象,CBase和2个子对象CSub1和CSub2。
鉴于我有一个指向CBase的指针,如果对象是CSub1或CSub2,我可以找到(很容易以便携式方式)。 (或者两者都没有,但在我的应用程序中总是
1或其他)
在Delphi中,您将使用IS操作,例如
如果myObject是CSub1然后
.....
有人能告诉我C ++ equivelent吗?
感谢
乔治
Hi,
I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.
Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its always
1 or the other)
In Delp you would use the IS operation, eg
if myObject IS CSub1 then
.....
Can someone tell me the C++ equivelent please?
thanks
George
#include< typeinfo>
//如果myObject的类型为CBase *,请使用* myObject
if(typeid(* myObject)== typeid(CSub1))
{
...
}
>
相当于IS。如果您可以通过常见的
虚拟方法调用解决此问题,那就更好了。
Yannick
#include <typeinfo>
// if myObject is of type CBase *, use *myObject
if (typeid(*myObject) == typeid(CSub1))
{
...
}
is the equivalent of IS. If you can resolve this problem by a common
virtual method call, it is better.
Yannick
George Styles写道:
George Styles wrote:
我有一个基础对象,CBase和2个子对象CSub1和CSub2。
鉴于我有一个指向CBase的指针,如果对象是CSub1或CSub2,我可以找到(很容易以便携式方式)。 (或者两者都没有,但在我的应用程序中它总是1或者其他)
在Delphi中,你会使用IS操作,例如
如果myObject是CSub1然后
.....
有人可以告诉我C ++ equivelent吗?
感谢
George
Hi,
I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.
Given that I have a pointer to CBase, can I find out (easily in a portable
way) if the object is CSub1 or CSub2. (or neither, but in my app its
always 1 or the other)
In Delp you would use the IS operation, eg
if myObject IS CSub1 then
.....
Can someone tell me the C++ equivelent please?
thanks
George
尝试
if(typeid(myObject)== typeid(CSub1 *))
....
else if(typeid(myObject)== typeid(CSub2 *))
....
-
要获得我真实的电子邮件地址,请删除两个onkas
-
Dipl.-Inform。 Hendrik Belitz
中央电子学院
研究中心Juelich
Try
if ( typeid( myObject ) == typeid ( CSub1* ) )
....
else if ( typeid( myObject ) == typeid ( CSub2* ) )
....
--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
非常感谢:)
乔治
我会用一个普通的虚拟解决方案
" Yannick Le goc" <乐*** @ imag.fr>在消息中写道
news:bu ********* @ trompette.imag.fr ...
Thanks for that :)
George
I would have resolved it using a common virtual
"Yannick Le goc" <le***@imag.fr> wrote in message
news:bu*********@trompette.imag.fr...
George Styles写道:
George Styles wrote:
我有一个基础对象,CBase和2个子对象CSub1和CSub2。
鉴于我有一个指向CBase的指针,我能找到吗? (如果对象是CSub1或CSub2,则很容易以
便携方式)。 (或者两者都没有,但是在我的应用中它的
总是1或者其他)
在Delphi中,你会使用IS操作,例如
如果myObject是CSub1然后
.....
有人能告诉我C ++ equivelent吗?
感谢
乔治
Hi,
I have a base object, CBase, and 2 sub-objects CSub1 and CSub2.
Given that I have a pointer to CBase, can I find out (easily in a portable way) if the object is CSub1 or CSub2. (or neither, but in my app its always 1 or the other)
In Delp you would use the IS operation, eg
if myObject IS CSub1 then
.....
Can someone tell me the C++ equivelent please?
thanks
George
#include< typeinfo>
//如果myObject的类型为CBase *,请使用* myObject
if(typeid(* myObject)== typeid(CSub1) )
{
...
}
等同于IS。如果您可以通过常见的虚拟方法调用解决此问题,那就更好了。
Yannick
#include <typeinfo>
// if myObject is of type CBase *, use *myObject
if (typeid(*myObject) == typeid(CSub1))
{
...
}
is the equivalent of IS. If you can resolve this problem by a common
virtual method call, it is better.
Yannick
这篇关于你怎么知道对象foo是否是CBar类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!