动态类型检查? [英] Dynamic Type Check?

查看:87
本文介绍了动态类型检查?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道我用Java做过这个,但是有没有办法在C ++中做到这一点?


类型B和C继承自类型A.


在一个函数中,我想检查一个A的实例(也可以是B或

C),看看它是什么,继续......类似于:


一件事;

if(A.typeof(B))

{

B somethingB = static_cast< B>(某事);

//与B成员做事情

}

else if(A.typeof (C))

{

C somethingC = static_cast< C>(某事);

//与C成员做事情

}

其他

{

//与A成员合作

}


我知道我可以用虚函数来完成类似的东西

(多态),但是上面可以用任何方式完成吗?


好​​奇。

Brian

解决方案

" Brian Genisio" ; <峰; br ********** @ yahoo.com>在消息中写道

news:40 ******** @ 10.10.0.241 ...

我知道我用Java做过这个,但有没有用C ++做的方法?

B类和C类继承自A类。

在一个函数中,我想检查A的一个实例(也可以是B或者C />,看看它是什么,继续前进...类似于:

一些东西;
if(A.typeof(B))
{
B somethingB = static_cast< B>(某事);
//与B成员做事情
}
如果(A.typeof(C))
{
C somethingC = static_cast< C>(某事);
//与C成员做事情
}


/
/ /与A成员一起做事


我知道我可以用虚函数来实现类似的东西
(多态),但是上面可以用任何方式完成吗?

只是好奇。
Brian




请参阅''typeid''运算符。


-

Elias


Brian Genisio写道:


我知道我用Java做过这个,但有没有办法用C ++做什么?

B和C类继承自A类。

在一个函数中,我想检查一个A的实例(也可以是B或
C),看看它是什么,然后继续......类似的东西:

一些东西;
if(A.typeof(B))
{
B somethingB = static_cast< B>(某事);
//与B成员做事情
}
如果(A.typeof(C))
{
C somethingC = static_cast< C>(某事);
//与C成员一起做事
}


//
与A成员一起做事
}

我知道我可以用虚函数来实现类似的东西
(多态),但是上面可以用任何方式完成吗?



不是你现在的方式。

''something''是一个A对象,它总会成为一个对象

无法用它做什么(A不是B或C)。


为了做你想做的事要做,那么你必须至少有
a指针或参考。


void foo(A& pSome)

{

//这里我有一个指向A

//的指针,但也可能是B或C对象


if(dynamic_cast< B *>(pSome)!= 0){

//它是B对象

B * = dynamic_cast< B *>(pSome);

//用它做点什么

}

else if(dynamic_cast< C *>(pSome) )!= 0){

//它是一个C对象

C * = dynamic_cast< C *>(pSome);

//用它做点什么

}

}


但是更好的方法是使用多态来做你想要的东西

来处理对象。

-

Karl Heinz Buchegger
kb******@gascad.at




" Karl Heinz Buchegger" < KB ****** @ gascad.at>在消息中写道

news:40 *************** @ gascad.at ...

Brian Genisio写道:


我知道我用Java做过这个,但是有没有办法在C ++中做到这一点?

B类和C类继承自A类。

在一个函数中,我想检查一个A的实例(也可以是B或
C),看它是什么,然后继续......类似于:

一个东西;
if(A.typeof(B))
{
B somethingB = static_cast< B>(某事);
//用B成员
}
如果(A.typeof(C))
{
C somethingC = static_cast< C>(某事);
//做事情C成员
}

{
//与A成员一起做事
}

我知道我可以做类似的事情具有虚函数
(多态),但以上可以用任何方式完成吗?

不是你现在这样做的方式。
''something''是A对象,它永远是一个A对象
无论你用它做什么(A不是B或C)。

为了做你想做的事,然后你必须至少有一个指针或参考。

void foo(A& pSome)
//
//这里我有一个指向A的指针//但它也可以是B或C对象

if(dynamic_cast< B *>(pSome)!= 0){
//它是B对象
B * = dynamic_cast< B *>(pSome);
//用它做点什么
}




这是更简单(和有效)写的:


if(B * bp = dynamic_cast< B *>(pSome))

{

}

else if(dynamic_cast< C *>(pSome)!= 0){
//它是C对象
C * = dynamic_cast< C *>(pSome);
//用它做点什么
}


但是使用多态来做你想做的事情要好得多/>与对象有关。

-
Karl Heinz Buchegger
kb ****** @ gascad.at



I know I have done this in Java, but is there a way to do it in C++?

Type B and C inherit from type A.

In a function, I want to check an instance of A (which can also be B or
C), to see what it is, and move on... something like:

A something;
if( A.typeof(B) )
{
B somethingB = static_cast<B>(something);
// Do things with the B members
}
else if ( A.typeof(C) )
{
C somethingC = static_cast<C>(something);
// Do things with the C members
}
else
{
// Do things with the A members
}

I know I can accomplish something similar with virtual functions
(polymorphism), but can the above be done in any way?

Just curious.
Brian

解决方案

"Brian Genisio" <Br**********@yahoo.com> wrote in message
news:40********@10.10.0.241...

I know I have done this in Java, but is there a way to do it in C++?

Type B and C inherit from type A.

In a function, I want to check an instance of A (which can also be B or
C), to see what it is, and move on... something like:

A something;
if( A.typeof(B) )
{
B somethingB = static_cast<B>(something);
// Do things with the B members
}
else if ( A.typeof(C) )
{
C somethingC = static_cast<C>(something);
// Do things with the C members
}
else
{
// Do things with the A members
}

I know I can accomplish something similar with virtual functions
(polymorphism), but can the above be done in any way?

Just curious.
Brian



Refer to the ''typeid'' operator.

--
Elias


Brian Genisio wrote:


I know I have done this in Java, but is there a way to do it in C++?

Type B and C inherit from type A.

In a function, I want to check an instance of A (which can also be B or
C), to see what it is, and move on... something like:

A something;
if( A.typeof(B) )
{
B somethingB = static_cast<B>(something);
// Do things with the B members
}
else if ( A.typeof(C) )
{
C somethingC = static_cast<C>(something);
// Do things with the C members
}
else
{
// Do things with the A members
}

I know I can accomplish something similar with virtual functions
(polymorphism), but can the above be done in any way?



Not the way you do it now.
''something'' is an A object and it will always be an A object
no metter what you do with it (an A is not a B or a C).

In order to do what you want to do, then you must at least have
a pointer or a reference.

void foo( A& pSome)
{
// Here I have a pointer to an A
// but it could also be a B or a C object

if( dynamic_cast< B* >( pSome ) != 0 ) {
// it is a B object
B* = dynamic_cast< B* >( pSome );
// do something with it
}
else if( dynamic_cast< C* >( pSome ) != 0 ) {
// it is a C object
C* = dynamic_cast< C* >( pSome );
// do something with it
}
}

But its much better to use polymorphism to do what you want
to do with the object.
--
Karl Heinz Buchegger
kb******@gascad.at



"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:40***************@gascad.at...

Brian Genisio wrote:


I know I have done this in Java, but is there a way to do it in C++?

Type B and C inherit from type A.

In a function, I want to check an instance of A (which can also be B or
C), to see what it is, and move on... something like:

A something;
if( A.typeof(B) )
{
B somethingB = static_cast<B>(something);
// Do things with the B members
}
else if ( A.typeof(C) )
{
C somethingC = static_cast<C>(something);
// Do things with the C members
}
else
{
// Do things with the A members
}

I know I can accomplish something similar with virtual functions
(polymorphism), but can the above be done in any way?

Not the way you do it now.
''something'' is an A object and it will always be an A object
no metter what you do with it (an A is not a B or a C).

In order to do what you want to do, then you must at least have
a pointer or a reference.

void foo( A& pSome)
{
// Here I have a pointer to an A
// but it could also be a B or a C object

if( dynamic_cast< B* >( pSome ) != 0 ) {
// it is a B object
B* = dynamic_cast< B* >( pSome );
// do something with it
}



This is more simply (and efficiently) written:

if( B* bp = dynamic_cast<B*>(pSome) )
{
}
else if( dynamic_cast< C* >( pSome ) != 0 ) {
// it is a C object
C* = dynamic_cast< C* >( pSome );
// do something with it
}
}

But its much better to use polymorphism to do what you want
to do with the object.
--
Karl Heinz Buchegger
kb******@gascad.at



这篇关于动态类型检查?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆