如何将指向对象的指针设置为Null? C ++

时间:2013-04-24 05:41:53

标签: c++ pointers null segmentation-fault

我正在制作一个roguelike游戏,我已经设置好了,所以在MOST上会有3个怪物出现在一个关卡上。所以我有三个指向怪物的指针。它们都以NULL开头。

在那个怪物死后,我应该如何将指针设置回NULL?或许我的想法错了?基本上我需要移除怪物,直到指针再生为另一个怪物。

我尝试过一些事情,但我只是不断遇到seg故障。

我尝试使用擦除,删除,只需将其设置为NULL,然后重新创建它。无法理解。

以下是我设置指针的方法:

CreatureFactory myCreatures = CreatureFactory::instance();
    Creature * pCreature1 = NULL;
    Creature * pCreature2 = NULL;
    Creature * pCreature3 = NULL;;

    ItemFactory myItems = ItemFactory::instance();
    Item * pItem1 = myItems.generateItem();

    dl.placeInGame(personPlaying,randomGen);
    int monsterCount = 0;
    int turnCount = 0;
    bool playingGame = true;
    char playerController;

    while (playingGame){
            if ((turnCount % 2 == 0) && (personPlaying.getHP() < personPlay$
                    personPlaying.generateHP();
            }
            if (((turnCount % 3) == 0) && (monsterCount < 2)){
                    if(pCreature1 == NULL){
                            pCreature1 = myCreatures.generateCreature(4);
                            dl.placeInGame(pCreature1,randomGen);
                            monsterCount += 1;
                    }
                    else if(pCreature2 == NULL){
                            pCreature2 = myCreatures.generateCreature(4);
                            dl.placeInGame(pCreature2, randomGen);
                            monsterCount += 1;
                    }
                    else if(pCreature3 == NULL){
                            pCreature3 = myCreatures.generateCreature(4);
                            monsterCount += 1;
                    }
            }
            dl.dump();
            personPlaying.dumpStats();

 CreatureFactory code..
 CreatureFactory & CreatureFactory::instance(){
    static CreatureFactory myObj;
    return myObj;
}

CreatureFactory::CreatureFactory(){
    m_mtRandom.seed( time(NULL) );
    fstream xmlFile;
    xmlFile.open("critters.xml");
    vector<XMLSerializable*> pObjects;
    parseXML(xmlFile, pObjects);

    XMLSerializable * pObject;

    for(auto it = pObjects.begin(); it != pObjects.end(); it++){
            pObject = (*it);
            Creature * pCreature = dynamic_cast<Creature*>(pObject);
            if (pCreature != NULL){
                    m_vCreatures.push_back(pCreature);
            }
    }
}

CreatureFactory::~CreatureFactory(){

}
    Creature * CreatureFactory::generateCreature(int iMaxLevel) {
    vector<Creature*> tempCreatures;

    for(auto it = m_vCreatures.begin(); it != m_vCreatures.end(); it++){
            if ((*it)->getLevel() <= iMaxLevel){
                    tempCreatures.push_back((*it));
            }
    }
    int randomCreature = (m_mtRandom() % (tempCreatures.size() - 1));


    Creature * pCreature = tempCreatures.at(randomCreature);

    Creature * pReturnValue = new Creature(*pCreature);

    return pReturnValue;
}

然后我做了大量的循环并使用了大量的类。这些怪物对象的指针通过大约6或7个类。其中一个我需要在HP击中0时设置为NULL。

由于

1 个答案:

答案 0 :(得分:3)

在c ++中,你不应该将指针设置为null,因为Object仍然存在于内存中,但没有任何对它的引用 - >内存泄漏。 相反,您应该删除对象:

delete pCreatureN;

这将基本上从内存中删除对象,并使指针成为空指针。 之后pCreatureN = NULL;没问题。