ptr_container

This is a discussion on ptr_container within the C++ Programming forums, part of the General Programming Boards category; Hello Suppose I have: Code: class object { public: boost::ptr_vector<some_object> m_vector; }; boost::shared_ptr<object> o(new object()); o->m_vector.pushback(new some_object()); I wonder if ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    ptr_container

    Hello

    Suppose I have:

    Code:
    class object {
    public:
         boost::ptr_vector<some_object> m_vector;
    };
    
    boost::shared_ptr<object> o(new object());
    o->m_vector.pushback(new some_object());
    I wonder if that will clean m_vector after smart pointer is deleted?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,806
    Once the o container goes out of scope, delete should be called on any object items stored. This will call the destructor for all these object items. Internally, the destructor for the object class will then call the destructor for any data members which means the m_vector member should be destructed and any some_object's contained within should be properly dealt with.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    I was confused by hk_mp5kpdw's response when I first read it, but now I see (and agree with) what was said.

    You do not need to do any extra work, everything will be cleaned up automatically.

  4. #4
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630
    Thanks.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21