boost :: ptr_vector问题

时间:2012-04-29 05:29:53

标签: c++ boost ptr-vector

我希望有一个boost::ptr_vector对象的多态指针(这个部分大部分都在工作),但是我遇到了一些功能问题。

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

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

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

编辑:我已经实施了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;
}

0 个答案:

没有答案