如何判断两个对象是否具有相同的类型? [英] How to judge if two objects have the same type?

查看:180
本文介绍了如何判断两个对象是否具有相同的类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的,


假设我有一个名为Obj的班级。


班级Obj

{} ;


和一个名为Shape的类,它来自Obj。


class形状:public Obj

{ };


一个名为Color的类,它也是从Obj派生的。


class颜色:public Obj

{};


我有两个对象。


Obj * pA =新的形状或颜色;

Obj * pB =新的形状或颜色;


如何知道它们是否具有相同的类型?


我感谢您的帮助。


水生

Dear All,

Assume I have a class named Obj.

class Obj
{ };

And a class named Shape which is derived from Obj.

class Shape: public Obj
{ };

And a class named Color which is also derived form Obj.

class Color: public Obj
{ };

I have two objects.

Obj *pA = new Shape or Color;
Obj *pB = new Shape or Color;

How can I know if they have the same type?

I appreciate your help.

Shuisheng

推荐答案



" shuisheng D' μà£o

"

"shuisheng D′μà£o
"

亲爱的所有人,


假设我有一个名为Obj的班级。


class Obj

{};


和一个名为Shape的类,它来自Obj。


class形状:public Obj

{};


还有一个名为Color的类,它也是从Obj派生的。


类颜色:public Obj

{};


我有两个对象。


Obj * pA =新形状或颜色;

Obj * pB =新形状或颜色;


如何知道它们是否具有相同的类型?


我感谢你的帮助。


水生
Dear All,

Assume I have a class named Obj.

class Obj
{ };

And a class named Shape which is derived from Obj.

class Shape: public Obj
{ };

And a class named Color which is also derived form Obj.

class Color: public Obj
{ };

I have two objects.

Obj *pA = new Shape or Color;
Obj *pB = new Shape or Color;

How can I know if they have the same type?

I appreciate your help.

Shuisheng



您是否考虑过RTTI?

Did you consider about RTTI?


shuisheng写道:
shuisheng wrote:

亲爱的所有人,


假设我有一个名为Obj的课程。


class Obj

{};


和一个名为Shape的类,它来自Obj。


class形状:public Obj

{};


和一个名为Color的类,也是从Obj派生的。


class颜色:public Obj

{};

我有两个对象。


Obj * pA =新形状或颜色;

Obj * pB =新形状或颜色;


我怎么知道他们是否有相同的类型?
Dear All,

Assume I have a class named Obj.

class Obj
{ };

And a class named Shape which is derived from Obj.

class Shape: public Obj
{ };

And a class named Color which is also derived form Obj.

class Color: public Obj
{ };

I have two objects.

Obj *pA = new Shape or Color;
Obj *pB = new Shape or Color;

How can I know if they have the same type?



从法律语法开始。


C ++在语法中内置了类型信息。你在那里写的是什么,这是不合法的,因此不是一个合理的问题。

它无法回答任何人都可以回答任何问题

非感性的问题。


你可能会问一些类似的东西(给你的例子类)

如果你有原型的功能:


void myFunc(Obj * pObj);


然后在函数体内你怎么能告诉

实际是什么传入的对象的类型是?


或者你可以写这样的东西(仅限伪代码):


if(复杂的东西,不能总是在编译时预测)

Obj * pA =新形状;

其他

Obj * pA =新颜色;


如何在此次运行后确定pA

指向的对象类型?


有两层答案:第一个是运行时类型信息。

查找。


第二层是,即使在这种情况下,大多数时候你也不应该问这个问题。如果你有一个继承,那么孩子班应该可以替代父类的

。查找Liskov替换。写得好

C ++继承将正常工作,无论pA实际指向的

对象的类型,只要它是公共继承

来自类型Obj。如果你认为你需要这些信息,你应该考虑重写所涉及的课程,这样你就可以b / b
不需要它。

袜子

Start with legal syntax.

C++ has type information built right into the syntax. What you
wrote there is not legal and is thus not a reasonable question.
It cannot be answered any more than one can answer any
non-sensical question.

You might ask something like (given your example classes)
if you had a function with prototype as such:

void myFunc(Obj *pObj);

then in the body of the function how could you tell what the
actual type of the object passed in was?

Or you could write something like this (psuedo code only):

if(complicated stuff that cannot always be predicted at compile time)
Obj *pA = new Shape;
else
Obj *pA = new Color;

How can you determine the type of the object pointed at by pA
after this runs?

There are two layers of answer: The first is "run time type info."
Look it up.

The second layer is, most of the time you shouldn''t be asking
this question even in this situation. If you''ve got an inheritance,
the child class is supposed to be able to substitute for
the parent class. Look up Liskov substitution. A well written
C++ inheritance will work properly regardless of the type of
object that pA actually points to, provided it is public inheritance
from the type Obj. If you think you need this info, you should
be considering a rewrite of the classes involved so that you
don''t need it.
Socks


if(typeof(pA)== typeof(pB){/ *相同* /}


或类似的东西:)

Puppet_Sock写道:
if( typeof(pA) == typeof(pB ) { /* the same */ }

or something like that :)
Puppet_Sock wrote:

shuisheng写道:
shuisheng wrote:

亲爱的所有人,


假设我有一个名为Obj的班级。


班级Obj

{};


和一个名为Shape的类,它来自Obj。


class形状:public Obj

{};


一个名为Color的类,也是从Obj派生的。


类颜色:public Obj

{};


我有两个对象。


Obj * pA =新形状或颜色;

Obj * pB =新形状或颜色;


我怎么知道他们是否有相同的类型?
Dear All,

Assume I have a class named Obj.

class Obj
{ };

And a class named Shape which is derived from Obj.

class Shape: public Obj
{ };

And a class named Color which is also derived form Obj.

class Color: public Obj
{ };

I have two objects.

Obj *pA = new Shape or Color;
Obj *pB = new Shape or Color;

How can I know if they have the same type?



从法律语法开始。


C ++在语法中内置了类型信息。你在那里写的是什么,这是不合法的,因此不是一个合理的问题。

它无法回答任何人都可以回答任何问题

非感性的问题。


你可能会问一些类似的东西(给你的例子类)

如果你有原型的功能:


void myFunc(Obj * pObj);


然后在函数体内你怎么能告诉

实际是什么传入的对象的类型是?


或者你可以写这样的东西(仅限伪代码):


if(复杂的东西,不能总是在编译时预测)

Obj * pA =新形状;

其他

Obj * pA =新颜色;


如何在此次运行后确定pA

指向的对象类型?


有两层答案:第一个是运行时类型信息。

查找。


第二层是,大多数即使在这种情况下你也不应该问这个问题。如果你有一个继承,那么孩子班应该可以替代父类的

。查找Liskov替换。写得好

C ++继承将正常工作,无论pA实际指向的

对象的类型,只要它是公共继承

来自类型Obj。如果你认为你需要这些信息,你应该考虑重写所涉及的课程,这样你就可以b / b
不需要它。

袜子


Start with legal syntax.

C++ has type information built right into the syntax. What you
wrote there is not legal and is thus not a reasonable question.
It cannot be answered any more than one can answer any
non-sensical question.

You might ask something like (given your example classes)
if you had a function with prototype as such:

void myFunc(Obj *pObj);

then in the body of the function how could you tell what the
actual type of the object passed in was?

Or you could write something like this (psuedo code only):

if(complicated stuff that cannot always be predicted at compile time)
Obj *pA = new Shape;
else
Obj *pA = new Color;

How can you determine the type of the object pointed at by pA
after this runs?

There are two layers of answer: The first is "run time type info."
Look it up.

The second layer is, most of the time you shouldn''t be asking
this question even in this situation. If you''ve got an inheritance,
the child class is supposed to be able to substitute for
the parent class. Look up Liskov substitution. A well written
C++ inheritance will work properly regardless of the type of
object that pA actually points to, provided it is public inheritance
from the type Obj. If you think you need this info, you should
be considering a rewrite of the classes involved so that you
don''t need it.
Socks


这篇关于如何判断两个对象是否具有相同的类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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