-
October 1st, 2009, 08:44 AM
#1
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.
-
October 1st, 2009, 09:19 AM
#2
Re: Doubt with object orientation on c++
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.
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.
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.
Kurt
-
October 1st, 2009, 09:19 AM
#3
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.
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
or maybe i have to delete the object separately?
I don't know what you mean by "delete the object separately".
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.
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.
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
-
October 1st, 2009, 10:50 AM
#4
Re: Doubt with object orientation on c++
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.
Last edited by nuzzle; October 1st, 2009 at 11:50 AM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|