1

我想要一个boost::ptr_vector指向对象的多态指针(这部分大部分都在工作),但是我在使用它的某些功能时遇到了麻烦。

我想加载ptr_vector使用 case 开关来确定要指向的子类型,这样我就不必让多个指针浮动,但是当我覆盖类型时,指针多态地指向它会覆盖其中的所有ptr_vector内容使用相同的指针推送(我认为这与它存储确切的指针有关)我认为可以通过给它对象的副本/克隆而不是指针来解决,但我看不到在哪里,或者如何告诉它如何做到这一点。

我遇到的另一个问题是,当指针存在的范围结束时ptr_vector停止存在的所有对象(我觉得这可以通过解决问题来解决是第 2 段)

我的另一个问题是如何在示例中删除特定对象,ptr_vector例如我发现当其中有 30 多个对象时我需要删除对象 5 ptr_vector(请记住,我将搜索要删除的对象)

编辑:我已经实现了这些clone方法,并且它们工作正常

编辑:示例代码

#include <iostream>
#include <string>
#include <vector>
#include <boost/ptr_container/ptr_vector.hpp>

gameObject(多态类型)

class GameObject {
public :
    bool toBeRemoved;
    char Type;
    int name;

    virtual ~GameObject(){}
    virtual void updateObject(float duration){}
    virtual GameObject * clone(void)const = 0;
};

class Entity : public GameObject{
public:
    Entity(int _name){
        Type = 'e'; name = _name;
        toBeRemoved = false;
    }
    Entity(const Entity & _o){
        toBeRemoved = _o.toBeRemoved;
    }
    ~Entity(){}
    void updateObject(float duration){
        if(!toBeRemoved){
//          perform update work
      }
    }
    Entity * clone(void)const{return new Entity(*this);}
};

class Wall:public GameObject{
public:
    bool destroyable;

    Wall(bool destroy, int _name){
        Type = 'w'; name = _name;
        destroyable = destroy;
        toBeRemoved = false;
    }
    Wall(const Wall & _o){
        destroyable = _o.destroyable;
    }
    void updateObject(float duration){
        if((duration > 10)&&(destroyable)){
            toBeRemoved = true;
        }
    }
    ~Wall(){}
    Wall * clone(void)const{return new Wall(*this);}
};

对象管理器(举例说明范围)

typedef boost::ptr_vector<GameObject*> things; // not sure how to tell this how to clone the objects, or if I have to do it manually

class ObjectMgr{
    things objects;
public:
    ObjectMgr(){
        int entityCount = 0;
        int wallCount = 0;

        std::vector<std::vector<char>> map;
        map.resize(3);
        for(int ii = 0; ii <3 ; ii++){map[ii].resize(6);}

        for(int ii = 0; ii<6; ii++){map[0][ii] = 'w';}
        map[1][0] = 'w'; map[1][1] = 'E';
        map[1][2] = '.'; map[1][3] = 'D';
        map[1][4] = 'E'; map[1][5] = 'D';
        for(int ii = 0; ii<6; ii++){map[2][ii] = 'w';}

        GameObject* currentObject;
        for(int zz = 0; zz < map.size(); zz++){
            for(int xx = 0; xx < map[zz].size(); xx++){

                switch(map[zz][xx]){
                case 'w':   // undestroyable wall
                    currentObject = new Wall(false,wallCount++);
                    objects.push_back(&currentObject);
                    break;
                case 'E':   // entity
                    currentObject = new Entity(entityCount++);
                    objects.push_back(&currentObject);
                    break;
                case 'D':   // destroyable wall
                    currentObject = new Wall(true,wallCount++);
                    objects.push_back(&currentObject);
                    break;
                case '.':   // empty space
                    break;
                }
            }
        }
        print();
    }

    void clean(){    // thinking that might need to do some kind of swapping, and then maybe pop_back()
        bool removed;
        do{
            removed = false;
            things::iterator ii = objects.begin();
//              for(int jj = 0; ii != objects.end(); ii++, jj++){
//                  if(objects[jj]->toBeRemoved == true){
//                      removed = true;
//                      // remove item here
//                      break;
//                  }
//              }
        } while(removed);
    }

    void print(){
        int count = 0;
        things::iterator reg = objects.begin();
        std::cout << "entities" << std::endl << "===================" << std::endl;
        for(int ii = 0; reg!=objects.end(); ii++, reg++){
            std::cout << count++ << ": " << objects[ii]->Type << objects[ii]->name << " " << std::endl;
        }
        std::cout << "====================" << std::endl << "/entities" << std::endl;
    }
};

主要的

int _tmain(int argc, _TCHAR* argv[]){
    ObjectMgr manager;
    std::cout << "constructed now displaying" << std::endl;
    manager.print();
    std::cout << "cleaning" << std::endl;
//  manager.clean();
    std:: cout << "displaying after cleaning" << std::endl;
//  manager.print();
    return 0;
}
4

0 回答 0