开发者

What would the destructor for this class look like?

开发者 https://www.devze.com 2022-12-28 10:23 出处:网络
class Equipment { std::vector<Armor*> vEquip; Weapon* mainWeapon; int totalDefense; int totalAttack;
class Equipment
{
    std::vector<Armor*> vEquip;
    Weapon* mainWeapon;
    int totalDefense;
    int totalAttack;

public:

    unsigned int GetWeight();

    int * GetDefense();

    bool EquipArmor(Armor* armor);
    bool UnequipArmor(Armor* armor);

    bool EquipWeapon(Weapon* wep);
    bool UnequipWeapon(Weapon* wep);

    Equipment();
    virtual ~Equipment();
};

It seems like there should be no destructor. The vector of pointers will take care of itself when it goes out of scope, and the actual objects the pointers p开发者_如何学Coint to don't need to be deleted as there will be other references to it.

All of the objects in this refer to the main Container:

class Container
{
    int weightLimit;
    unsigned int currWeight;
    std::vector<Item*> vItems;

public:

    bool AddItem(Item* item);
    bool RemoveItem(Item* item);

    Container();
    Container(int weightLim);
    Container(int weightLim, std::vector<Item*> items);
    ~Container();
};

Now here I can see it being necessary to delete all objects in the container, because this is where all the objects are assigned via AddItem(new Item("Blah"))

(Armor and Weapon inherit from Item)


Non-pointer types will take care of themselves. So, ints, floats, objects, etc. will take care of themselves, and you don't have to worry about deleting them.

Any pointers that are managed by the class need to be deleted by that class' destructor. So, if the memory pointed to by the pointer was allocated in the class or if it was given to that class with the idea that that class would manage it, then the destructor needs to delete the pointer.

If the pointer is to memory that another class is managing, then you obviously don't want your destructor to delete it. That's the job of the class that is in charge of the memory pointed to by that pointer.

Standard containers do not manage the pointers that they hold if they hold pointers. So, if you have a container of pointers, whichever class is supposed to manage them needs to delete them. Odds are that that's the class that holds the container, but that depends on what your code is doing.

So, typically, for a class that has a container of pointers, you'll need something like this in the destructor:

for(containerType<T*>::iterator iter = container.begin(),
                                end  = container.end();
    iter != end;
    ++iter)
{
    delete *iter;
}

Every pointer that has memory allocated to it has to have something (generally a class, but sometimes the function that it's allocated in) who effectively owns that memory and makes sure that it is freed. When talking about classes, that's usually the same class that the memory is allocated in, but of course, it's quite possible for one class or function to allocate the memory and then effectively pass on the ownership of that memory to another class or function. Regardless, whoever "owns" the memory needs to deal with cleaning it up. That's what your destructor needs to worry about: cleaning up any resources that that class owns.


If you need to delete the items, then:

Container :: ~Container() {
   for ( unsigned int i = 0; i < vItems.size(); ++i ) {
      delete vItems[i];
   }
}

Note that for this to work correctly, the Item class must have a virtual destructor, if the array really contains pointer to Armour and Weapon instances.


If I understand you correctly, you're asking how to delete items from the vItems vector in the main container destructor?

for(std::vector<Item*>::iterator i=vItems.begin(),ie=vItems.end();i!=ie;++i)
    delete *i;

Is that what you are asking for?


If your vector would be

std::vector<std::tr1::shared_ptr<Item> >

, then you wouldn't need a destructor. As it is now, the destructor would have to iterate over the vector to delete all items. Easiest with BOOST_FOREACH.


It depends on how you implement it. If the items you add are managed from outside your object (i.e. the body of addItem() looks something like this: { vItems.push_back(items); }), then you won't need a destructor for that one. However, if the items are managed by the object (i.e. addItem() looks like this: { vItems.push_back(new Item(item)); }, then you will need to delete all items in the destructor, because nobody will do that for you.

0

精彩评论

暂无评论...
验证码 换一张
取 消