Doubt with object orientation on c++
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: Doubt with object orientation on c++

  1. #1
    Join Date
    Jul 2009
    Posts
    8

    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.

  2. #2
    Join Date
    Oct 2002
    Location
    Austria
    Posts
    1,284

    Re: Doubt with object orientation on c++

    Quote Originally Posted by dianyu View Post
    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 View Post
    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 View Post
    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 View Post
    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

  3. #3
    Join Date
    Apr 1999
    Posts
    27,446

    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 View Post
    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?
    Code:
    p.~person();
    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

  4. #4
    Join Date
    May 2009
    Posts
    2,413

    Re: Doubt with object orientation on c++

    Quote Originally Posted by dianyu View Post
    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 12:50 PM.

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center