mrm9084
mrm9084

Reputation: 493

Making Lists of **Pointers in c++

What I was trying to do was make a list of them with

Item *itm = new Item();
_lst.push_front(&itm);

lst was made made in the header

std::list<int> _lst;

I am trying to learn c++ on my own so any advise would be great.

edit1) doesn't &itm give the pointers location in memory which would be an int?

edit2) The fact that i should not be learning c++ on my own does not help with the pointer problem

Upvotes: 0

Views: 87

Answers (1)

batbrat
batbrat

Reputation: 5221

I'd suggest simply using

std::list<Item**> _lst;

Do you need the double indirection? Can't you do with a list like this:

std::list<Item*> _lst;

Of course, you'd have to push back itm instead of &itm if you wanted to do that. Are you sure you need a list and not a vector or a deque? As the comments suggested, get a good C++ book to help things along.

After reading the comment on my post by R. Martinho Fernandes, I have to suggest std::list<Item> _lst; as a good alternative as well. It really depends on what you need, as I said earlier.

I hope this helps.

Upvotes: 2

Related Questions