Adding an object to an Deque

I was making a text based game.
I had created a class Inventory, which contained objects that can be carried.

Now I want to create a Deque to add and remove objects of the type Inventory.

Now if I don't declare objects of Inventory, is it possible that I create an object in Inventory?

I am adding things like this:
1
2
deque<Inventory*> inven;
inven.push_back(new Inven);


Tell me is the=is the correct way?

The other problem is that I have a static variable that will tell the number of objects int he inventory.

Creating objects like I do above increased the object count (which is incremented in the Constructor).

But simply removing an object from the deque (using pop_front & pop_back) doesn't call the destructor (which decrements the number of objects in the Inventory).

So is there a proper way to do this properly?
pop_back calls the destructor of the value stored in the container. That value is a pointer and pointers don't have destructors. You'd have to call delete to destroy the object it points to.
Note that you don't have to use pointers.
1
2
deque<Inventory> inven;
inven.push_back(Inven());
would work as well.
Unless you intend to use polymorphism or want to have other pointers to these items.
Last edited on
Tell me is the=is the correct way?
Yes it is.

So is there a proper way to do this properly?
You need to delete the object before pop_...() is called like
1
2
delete inven.front();
inven.pop_front();
@hamsterman:
I tried it the way you gave. The result is that it gives the error:
term does not evaluate to a function taking 0 arguments


By the way, I had made a mistake in the first post:
In line 2, it is new Inventory.
The error occurs even if I make an object Inven and do as you suggested...

BTW, I am not using pointers. I guessed that using pointers would ensure calling the constructor for every object I added.

@coder777:
That worked greatly.

Thanks a lot both of you.

Just one more question, is there a better STL Container which allows us to remove something from any location of the Inventory (Container)?

currently I am using Swap to move the specific object to front, and then removing it.

Again Thanks a lot.
I tried it the way you gave. The result is that it gives the error:
I don't see why that could be happening. Provided that you fixed my copy of your typo too..

Try compiling this snippet, and see what happens:
1
2
3
4
5
6
7
8
9
10
#include <deque>

struct Inventory{
   int stuff;
};

int main() {
   std::deque<Inventory> inven;
   inven.push_back( Inventory() );
}


I am not using pointers
That * after "Inventory" in line "deque<Inventory*> inven;" means a pointer, so does "new".

is there a better STL Container which allows us to remove something from any location of the Inventory (Container)?
deque does allow that. See http://www.cplusplus.com/reference/stl/deque/erase/
Last edited on
I definitely need some sleep.
I am not using pointers


I had planned on writing Polymorphism...


Ah! Thanks.

I hadn't noticed that.

Thanks again!
Tried the snippet out.
Changed the typo and it worked as well.
Thanks a lot.
If you need to store pointers (when using polymorphism), you can use a ptr_deque or (with C++11 support), a deque<unique_ptr<Inventory>> in order to conform to RAII.
Don't even start with manual deleting before erasing. It's a huge mess which will only grow into a full-blown disaster as your program grows.
Last edited on
Topic archived. No new replies allowed.