|
This thread is locked; no one can reply to it. |
1
2
|
STL and the map template |
Don Freeman
Member #5,110
October 2004
|
Ok...I've not really ever messed with the STL map template, so I may be doing this wrong... My problem is that the destructor for Tile is NOT being called! I have read the description for map, but it does not call the destructor(s) as it should for some reason!
Probably something I've done that is stupid....hehehe As always...Thanks in advance, -- |
BAF
Member #2,981
December 2002
|
IIRC, clear just clears the map, not calling the destructor or anything. You will have to iterate through the map yourself, deleting each pointer stored. |
Don Freeman
Member #5,110
October 2004
|
map-C++ Reference said: Clear content All the elements in the container are dropped: their destructors are called, and then they are removed from the container, leaving it with a size of 0. Maybe I am just confused as to what/who's destructor clear is calling???? -- |
BAF
Member #2,981
December 2002
|
A pointer doesn't have a deconstructor though. If they were just tiles (not tile*s) then it would work as you are expecting. |
Don Freeman
Member #5,110
October 2004
|
So...the clear function does not dereference the pointers (as in this case?) How would I add elements to the map without having to worry about them going out of scope? ... void SomeFunction( map<int,Tile> &tiles ) { Tile aTile; tiles.insert(pair<int,Tile>0,aTile); } ... Would that be acceptable? I thought that since this would go out of scope, the map class would not contain valid data anymore...or is it that it is calling a copy constructor for the temporary? [Edit 1] -- |
X-G
Member #856
December 2000
|
That's right. If you had been storing objects instead of pointers, the destructors would've been called; but the way you've done it now, you need to go through it. EDIT: As for your reply there, yes, that's valid. There'll be a copy of the object in the map. -- |
Don Freeman
Member #5,110
October 2004
|
Thanks to all... I get confused with pointers every once in a while for some stupid reason.... I don't know why I let them confuse me so... Working insert without calling copy constructor:
-- |
X-G
Member #856
December 2000
|
Uh... that's not a good idea. That's an access violation waiting to happen. -- |
BAF
Member #2,981
December 2002
|
That's going to give you invalid pointers as soon as pTile goes out of scope. The easiest solution, IMO, would be to do what you were doing before and just to iterate the map and delete each object. |
Don Freeman
Member #5,110
October 2004
|
I was hoping for some kind of "automatic" cleanup when the map object goes out of scope, but I guess I'll have to manually delete all the objects....damn! Again, Thanks to X-G and BAF, -- |
X-G
Member #856
December 2000
|
There is such a thing: store objects instead of pointers. -- |
Don Freeman
Member #5,110
October 2004
|
Ok then...two part question... One...how would I do THAT (store objects instead of pointers)? map<int,Tile*>::iterator it; for ( it=tiles.begin(); it != tiles.end(); it++ ) { delete (&it); }
-- |
X-G
Member #856
December 2000
|
1) map<int, Tile> 2) No. delete (*it); -- |
Don Freeman
Member #5,110
October 2004
|
I tried your delete (*it)...I got this error: error: type ‘struct std::pair<const int, Tile*>’ argument given to ‘delete’, expected pointer
Second:
-- |
X-G
Member #856
December 2000
|
Quote: I got this error: Oops, sorry. I keep forgetting that map iterators aren't like the other ones. I think it's i->second() or something like that. Check the manual. Quote: I don't really want that: There's no way around it. Write your constructors and destructors to cope with it. (Also, maps overload the [] operator: tiles[0] = Tile(...);) -- |
Don Freeman
Member #5,110
October 2004
|
Well, for just one add operation...I get 5 calls to the destructor on exit! That seems like crap! I might just end up using a list instead. -- |
X-G
Member #856
December 2000
|
Lists aren't all that much better. You're still going to have to write proper ctors/dtors... that goes for all STL types. -- |
Don Freeman
Member #5,110
October 2004
|
Yeah....noticed that with the list class as well... Damn it...I don't like all the extra calls to the copy constructor and the extra calls to the destructor....such is life I guess....Thanks for the help X-G! -- |
Kibiz0r
Member #6,203
September 2005
|
Why were smart pointers not mentioned, this whole time? You want a pointer that will call the destructor of the object that it points to as soon as it goes out of scope, yeah? y halo thar, Smart Pointers. (part of Boost) Edit: Oh, okay, nevermind, cookies were already given. Then I retract my help, seeing as it's not wanted. --- |
Carrus85
Member #2,633
August 2002
|
Kibiz0r: I'm guessing because smart_ptr isn't part of the standard library (auto_ptr is, but it doesn't like being placed into a container).
|
Kibiz0r
Member #6,203
September 2005
|
Well, you can write your own template easy enough. --- |
gillius
Member #119
April 2000
|
I second the smart pointer solution. Spending so much time to avoid using a tool that works... Gillius |
X-G
Member #856
December 2000
|
I figure it was a good opportunity for him to learn how these things work. -- |
Thomas Fjellstrom
Member #476
June 2000
|
Quote: I figure it was a good opportunity for him to learn how these things work. I for one agree with you. -- |
ImLeftFooted
Member #3,935
October 2003
|
Quote: Also, maps overload the [] operator: tiles[0] = Tile(...);
Insert is a bit faster I think (depending on the speed of your default ctor). tiles.insert ( pair<int,Tile>(0,pTile) ); Its a bit easier / cleaner to use make_pair tiles.insert ( make_pair(0,pTile) );
Quote: auto_ptr ... doesn't like being placed into a container. Hm? I was always under the impression that it would go into a container just fine. |
|
1
2
|