简体   繁体   中英

How to access an interface-implementing object stored as pointer in an std::vector

So I have this:

std::vector<EnemyInterface*> _activeEnemies;

where EnemyInterface looks like this:

#include "Ogre.h"

class EnemyInterface{
public:
  virtual void update(const Ogre::Real deltaTime) = 0;
  virtual void takeDamage(const int amountOfDamage, const int typeOfDamage) = 0;
  virtual Ogre::Sphere getWorldBoundingSphere() const = 0;
  virtual ~EnemyInterface(){} 
};

I create a new enemy:

// Spikey implements EnemyInterface
activeEnemies.push_back( (EnemyInterface*) &Spikey(_sceneManager, Ogre::Vector3(8,0,0)) );

And I want to call the update function on every enemy, but it crashes:

// update enemies
for (std::vector<EnemyInterface*>::iterator it=_activeEnemies.begin(); it!=_activeEnemies.end(); ++it){
        (**it).update(timeSinceLastFrame); // Option 1: access violation reading location 0xcccccccc
        (*it)->update(timeSinceLastFrame); // Option 2: access violation reading location0xcccccccc
    }

I can see the enemy on screen, but I cannot access it. Any help would be appreciated.

Spikey.h looks like this:

#include "EnemyInterface.h"

class Spikey: virtual public EnemyInterface{
private:
int thisID;
static int ID;

Ogre::SceneNode* _node;
Ogre::Entity* _entity;
public:
Spikey(Ogre::SceneManager* sceneManager, const Ogre::Vector3 spawnPos);

// interface implementation
virtual void update(const Ogre::Real deltaTime);
virtual void takeDamage(const int amountOfDamage, const int typeOfDamage);
virtual Ogre::Sphere getWorldBoundingSphere() const;
};

It's because you create a temporary object in your push_back call. As soon as the push_back function returns that object is no more, and leaves you with a dangling pointer.

You have to create a new object using new instead:

activeEnemies.push_back(new Spikey(_sceneManager, Ogre::Vector3(8,0,0)));

change

activeEnemies.push_back( (EnemyInterface*) &Spikey(_sceneManager, Ogre::Vector3(8,0,0)) );

to

activeEnemies.push_back( new Spikey(_sceneManager, Ogre::Vector3(8,0,0)) );

And this is the correct call

(*it)->update(timeSinceLastFrame);

Your vector contains EnemyInterface* .

So *it gives you EnemyInterface* - ie a pointer to EnemyInterface. You can call a method using pointer to an object by using ->

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM