虚函数:对向量进行迭代<基类>它由子类对象填充 [英] Virtual Functions: Iterating over a vector<Base Class> that is populated with subclass objects

查看:67
本文介绍了虚函数:对向量进行迭代<基类>它由子类对象填充的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

简短描述:

我迭代一个向量,在向量中的每个对象上调用一个虚函数,以便执行一系列动作。向量是基类的迭代器。所有的对象都是孩子。当虚拟函数被调用时,它执行基类的函数。

Short Description:
I am iterating over a vector calling a virtual function on every object in the vector in order to execute a sequence of actions. The vector is of the base class as is the iterator. All the objects are children. When the virtual function is called it executes the base class's function.

(真的)长说明:
我试图建模一个生物,行为。我的基类是抽象的,只有两个函数(虚拟)所有的子类都重写:

(Really) Long Description: I am attempting to model a creature that has a set of behaviors. My base class is abstract with only two functions (virtual) which all the subclasses have overridden:

class Behavior
{
public:
     Behavior();
    ~Behavior(void){}
 virtual void execute(){} 
 virtual BEHAVIOR_TYPE getType() {return m_Type;}


protected:
BEHAVIOR_TYPE m_Type;
};

我创建了许多儿童行为,例如移动,消费,侦察等。 p>

I have created a number of children behaviors, such as move, consume, scout, etc.

class Move :
    public Behavior
{
public:
BEHAVIOR_TYPE getType() {return m_Type;}
    enum Direction {N, NE, E, SE, S, SW, W, NW};
Move(DOCO * d);
~Move(void);
void execute() ;
    Direction chooseDirection();
    void setDirection(Direction newDirection);
private:
    Direction m_Direction;
    DOCO *I;
BEHAVIOR_TYPE m_Type;

};

我创建了一个向量,我推送每个Behavior子类的实例以及遍历它的迭代器:

I created a vector onto which I pushed instances of each Behavior subclass as well as an iterator to traverse it:

vector<Behavior> m_Behavior;
vector<Behavior>::iterator bIt;

当生物获取动作序列时,我尝试遍历向量,解引用迭代器,调用execute函数:

When the creature gets an action sequence, I try to iterate over the vector, dereference the iterator, and call the execute function:

void World::justDoIt()
{
    for(dIt=myDOCO.begin(); dIt!=myDOCO.end(); ++dIt)
{
    vector<Behavior>::iterator myBehavior=(dIt)->getFirstBehavior();
    vector<Behavior>::iterator end=(dIt)->getLastBehavior();
    for(myBehavior; myBehavior!=end; ++myBehavior)

        (*myBehavior).execute();
}
}

问题是它执行父的函数,而不是孩子的功能。

The problem is that it executes the parent's function rather than the child's function.

在我对后期绑定的理解中,它应该基于调用它的对象的类型而不是调用它的指针的类型来自动调用相应的函数,在我的代码中,我预计它会指向子对象。

In my understanding of late binding it should automatically call the appropriate function based on the type of object that was calling it rather than the type of pointer with which it was called, and in my code I anticipated it would be pointing to the child object.

显然,我有一个错误,不知何故告诉程序,我想要这些被视为父母而不是孩子,但我找不到我的错误。

Obviously I have made an error and somehow told the program that I want these to be treated as parents instead of children, but I cannot find my error.

第二个症状是,它不会让父母的功能纯虚,因为它说它不能实例化一个抽象类。我不是在我的代码中的任何地方实例化它,但必须有一个地方我隐约地做。我找不到在哪里,但是。当然,创建一个向量来保存父类的对象不需要实例化父对象,这是我直接引用父类的唯一时间。

A second symptom is that it will not let me make the parents function pure virtual, because it says it cannot instantiate an abstract class. I am not instantiating it explicitly anywhere in my code, but there must be somewhere I am doing it implicitly. I cannot find where, however. Certainly creating a vector to hold objects of the parent class does not require instantiating the parent, and that is the only time I reference the parent class directly.

任何帮助将是非常感谢。

Any help would be greatly appreciated.

推荐答案

向量<行为> 无论你在里面存储什么,使用复制构造函数 Behavior :: Behavior(const Behavior&); 。这破坏了多态性。您需要在容器中使用指针或智能指针:

The class vector<Behavior> makes copies of whatever you store inside it, using the copy constructor Behavior::Behavior(const Behavior&);. This destroys the polymorphism. You need to use a pointer or smart pointer in the container instead:

vector<Behavior*> m_Behavior; // I will take care of new and delete
vector<shared_ptr<Behavior> > m_Behavior; // easier

如果没有 std :: shared_ptr std :: tr1 :: shared_ptr #include< memory> 你可以使用Boost的。

If you don't have std::shared_ptr or std::tr1::shared_ptr in #include <memory> or similar, maybe you can use Boost's.

这篇关于虚函数:对向量进行迭代&lt;基类&gt;它由子类对象填充的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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