Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 769219
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T18:18:06+00:00 2026-05-14T18:18:06+00:00

class Equipment { std::vector<Armor*> vEquip; Weapon* mainWeapon; int totalDefense; int totalAttack; public: unsigned int

  • 0
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 point 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)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T18:18:07+00:00Added an answer on May 14, 2026 at 6:18 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

class Widget; std::vector< std::shared_ptr<Widget> > container class Criterium { public: bool operator()(const Widget& left,
class X { int i; public: X() { i = 0; } void set(int
class a{ public void foo(int a){ System.out.println(super); } } class b extends a{ public
class A{ virtual int foo1(int a){ return foo1_1(a,filler(a)); } template<typename FunctionPtr_filler> int foo1_1(int a,
class Test { public static void main(String[] args) throws Exception { Test t =
class EverythingMustBeInAClass { private final int i = 42; private final int[] a =
Please explain me why that code: class kategoria { public $IdKat; public $NazwaKat; public
class LinkedList{ private $first; public function Merge(LinkedList $ll){ //We can't access $ll->first for merging
class GoogleMusicAdapter extends TestFragmentAdapter implements TitleProvider { public GoogleMusicAdapter(FragmentManager fm) { super(fm); } @Override
I have three classes public class OSiteEquipment : IPlantItem public class OSiteSubSystem : IPlantItem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.