本文介绍了C ++中是否可能有多态成员重载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在研究Roguelike,但遇到了问题.我的问题是我想使用多态重载"或排序,但是我猜想C ++不支持.

I have been working on a Roguelike, and run into a problem with it. My problem is that I would like to use "polymorphic overloading" or sorts, but I'm guessing C++ doesn't support.

我的班级图是这样的:

xMapObject<-xEntity<-xVehicle

xMapObject <- xEntity <- xVehicle

一个问题是,可能有这个问题:

An the problem is, it is possible to have this:

class xMapObject
{
  public:
    virtual void Bump(xMapObject *MapObject);
    virtual void Bump(xEntity *Entity);
    virtual void Bump(xVehicle *Vehicle);

    virtual void BumpedBy(xMapObject *MapObject);
    virtual void BumpedBy(xEntity *Entity);
    virtual void BumpedBy(xVehicle *Vehicle);
};

这将是一个很好的选择,因为它将极大地简化确定谁陷入困境的代码,但是由于这行不通,因此是否存在另一种类似于此的面向对象的方法?还是选择对象的最佳选择来确定它们是什么?

This would be a very nice, as it would great simplify the code that determines who bumps into what, but since this doesn't work, is there another object oriented approach similar to this? Or is the best option casting objects to determine what they are?

感谢您的帮助!

推荐答案

确定有效.我认为,尽管您希望它能够在传递xMapObject *时分辨出差异,但这根本不会发生.

Sure it works. I think though that you expect it to be able to tell the difference when you pass it an xMapObject* and that simply won't happen.

您需要双重调度机制.也许是访客,但我对此表示怀疑.有关多种方法,请参见现代C ++设计或Wiki.

You need a double dispatch mechanism. Perhaps visitor but I'm doubting it. See Modern C++ Design or wiki for multimethods.

这篇关于C ++中是否可能有多态成员重载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 05:10