Doubt with object orientation on c++
Hello, i have some doubts with c++.
First of all, if i have a map of objects like this:
Code:
class People {
map<int,Person> mapPersons;
string city;
int code;
}
class Person{
int id;
string name;
string phone;
}
When i want to delete the object Person of an instance of the mapPersons, it is correct the next form:
Code:
map<int,Person>::iterator iter=_socis.begin();
mapPersons.erase(iter);
or maybe i have to delete the object separately?
How can i delette an object of the class People?Do i have to delete the map first?
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?
How can i destroy objects?
Code:
//created like:
Person p = Person()
//to destroy?
p.~person();
Everything thinking than the destroyers are like this:
Code:
~People(){
}
~Person(){
}
Thank you very much!!!
Imanol.
Re: Doubt with object orientation on c++
Quote:
Originally Posted by
dianyu
When i want to delete the object Person of an instance of the mapPersons, it is correct the next form:
Code:
map<int,Person>::iterator iter=_socis.begin();
mapPersons.erase(iter);
or maybe i have to delete the object separately?.
No. erase will call the destructor for you.
Quote:
Originally Posted by
dianyu
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.
Quote:
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
Quote:
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.
Kurt
Re: Doubt with object orientation on c++
Before answering, have you tried any of the code you posted? It would be better if you try first, and then come back with questions.
Quote:
Originally Posted by
dianyu
First of all, if i have a map of objects like this:
Code:
class People {
map<int,Person> mapPersons;
string city;
int code;
}
class Person{
int id;
string name;
string phone;
}
When i want to delete the object Person of an instance of the mapPersons, it is correct the next form:
Code:
map<int,Person>::iterator iter=_socis.begin();
mapPersons.erase(iter);
There are 3 erase() functions for map. The first takes an iterator, the second takes a key, and the third takes two iterators defining a range.
http://www.sgi.com/tech/stl/Map.html
Quote:
or maybe i have to delete the object separately?
I don't know what you mean by "delete the object separately".
Quote:
How can i delette an object of the class People?Do i have to delete the map first?
Remove the entry from the map. That's all you need to do.
Quote:
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?
Those objects are not deleted by you -- they are objects, therefore they are deleted whenever they go out of scope.
Quote:
How can i destroy objects?
Code:
//created like:
Person p = Person()
//to destroy?
You should not be destroying objects yourself this way. When the object goes out of scope, it is automatically destroyed.
You should get a good C++ book, since all of these concepts are better discussed in a book (and are fundamental concepts of C++).
Regards,
Paul McKenzie
Re: Doubt with object orientation on c++
Quote:
Originally Posted by
dianyu
Hello, i have some doubts with c++.
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.