有没有办法推断对象的类型? [英] Is there a way to infer the type of an object?

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

问题描述

这可能是一个愚蠢的问题,我怀疑我知道答案(没有),因为我似乎在这里打墙。



从某些类派生的对象:

  class BaseClass; 
class DerivedA:public BaseClass;
class DerivedB:public BaseClass;
class DerivedC:public BaseClass;
std :: vector< BaseClass> myCollection;

我想根据特定类的类型调用方法:

  class Processor {
void doSomething(DerivedA a,DerivedB b);
void doSomething(DerivedA a,DerivedC c);问题是,如果我访问集合中的单个项目,并尝试使用这个方法来创建一个新的集合。调用'处理器'中的'doSomething'方法,它将不能决定使用哪个方法(afaik)。所以我的问题是:是否有任何方法来获取正确的派生类型的集合中的项目?

解决方案

将保持 doSomething 方法,这是所谓的 multiple dispatch ,目前不支持C ++。



如果它是 BaseClass 的一个虚拟成员函数,那么是它的工厂C ++多态的运行它是被调用的对象,但它仍然不会自动推断争论的类型。



为了解决这个问题,你可以做一些类似于

  void collideWith(Thing& other){
// dynamic_cast为指针类型失败
//(对于引用类型的dynamic_cast会在失败时抛出异常)
if(Asteroid * asteroid = dynamic_cast< Asteroid *>(& other)){
// handle小行星碰撞
} else if(Spaceship * spaceship = dynamic_cast< Spaceship *>(& other)){
//处理小行星 - 太空船碰撞
} else {
//默认碰撞处理在这里
}
}

各种可能的Derived类,直到一个工作,并适当地调用其中一个方法(没有特殊的努力,因为编译器知道你试图投射到什么类型)。



重要提示:正如@WhozCraig指出,您的向量需要保存指针,以避免 Object-Slicing ,并将整个问题还原。


This may be a stupid question, I suspect I know the answer (no) because I seem to be hitting a wall here.

Given I have a collection of objects derived from certain class:

class BaseClass;
class DerivedA: public BaseClass;
class DerivedB: public BaseClass;
class DerivedC: public BaseClass;
std::vector<BaseClass> myCollection;

I want to call a method depending on the types of the specific class:

class Processor {
  void doSomething(DerivedA a, DerivedB b);
  void doSomething(DerivedA a, DerivedC c);
}

The problem is, if I access the individual items on the collection and try to call the 'doSomething' method in the 'Processor', it will not be able do decide which method to use (afaik). So my question is: Is there any way to fetch the items in the collection with the right derived-type?

解决方案

If you are going to keep the doSomething method as it is, this is what is called multiple dispatch and is NOT currently supported by C++.

If it were a virtual member function of BaseClass then yes it would be the run of the mill C++ polymorphism on the object it is being invoked on, but it would still NOT automatically infer the type of the arguement.

To get around this you can do something like what is suggested in the earlier link

void collideWith(Thing& other) {
     // dynamic_cast to a pointer type returns NULL if the cast fails
     // (dynamic_cast to a reference type would throw an exception on failure)
     if (Asteroid* asteroid = dynamic_cast<Asteroid*>(&other)) {
         // handle Asteroid-Asteroid collision
     } else if (Spaceship* spaceship = dynamic_cast<Spaceship*>(&other)) {
         // handle Asteroid-Spaceship collision
     } else {
         // default collision handling here
     }
 }

Basically keep casting to various possible Derived classes until one works and call one of the methods appropriately(no special effort since the compiler knows what type you are trying to cast to).

IMPORTANT: as @WhozCraig points out, your vector needs to hold pointers to avoid Object-Slicing and render this whole question moot.

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

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