How can i delette an object of the class People?Do i have to delete the map first?
No the destructor of People will take care of that.
Originally Posted by dianyu
If i want to clear the content of a map, with the function clear() the objects will be destroyed like if i call delete to all the objects?
Yes
Originally Posted by dianyu
How can i destroy objects?
Code:
//created like:
Person p = Person()
//to destroy?
p.~person();
You never call constructors or destructors directly.
Create an object like that
Code:
//created like:
Person p; // declaring the object p will call the constuctor Person::Person()
Does that even compile ?
Code:
//to destroy?
p.~person();
When the object falls out of scope the destructor is called automatically.
The only time you have to worry about destroying objects is when you create an object by using new. For every new there must be a corresponding delete.
New will call the constructor and delete will call the destructor automatically.
You're using the Person class as a value object. This means it can be treated just like any other value object, like for example an int. So in order not to get confused it may be instructive to replace Person with the simpler int like,
map<int, int> mapPerson;
Then you learn how how to handle that map and when you feel confident you know how it works you put Person back in. It will work exactly the same. In fact the main purpose of designing a class to be a value object is to make it behave like a primitive, such as an int.
Last edited by nuzzle; October 1st, 2009 at 11:50 AM.
Bookmarks