Thread: vectors of 'new' classes

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    36

    vectors of 'new' classes

    OK suppose I had a code like this

    Code:
    vector<base *> vec;
    
    vec.push_back(new derived());
    
    delete vec[0];
    Code like this does compile, but I don't think I'm grasping this correctly. Basically I'd like to have a container of new classes, and be able to access them (let's say 4 different classes all pushed onto the vector). My concerns are, does this cause any sort of memory leaks (let's say even with the class destructors being all in order). Does this just make a copy of the derived classes? From what I can understand the vector elements are just pointers to whatever objects.

    Am I overthinking this?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I presume you wrote new derived() rather than just derived(). If so, then the approach works, except that you have to do manual memory management. The derived class objects are not copied; pointers to these objects are copied.

    To avoid possible mistakes due to manual memory management, it would be better to use a std::vector<std::shared_ptr<base>>, std::vector<std::unique_ptr<base>> or boost::ptr_vector<base> instead, depending on your requirements.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You will have to manage the vector just like you would manage any other newed pointer. So, you're not over thinking it.

    Additionally, smart pointers such as std::shared_ptr (requires C++11 support) or boost::shared_ptr will serve as a container for your pointer, so you are over thinking it.

    Additionally, unless you are using polymorphism, you will have to cast the pointer with dynamic_cast to call derived methods. So you're not over thinking it.

    Additionally, polymorphism is a wonderful tool, so if you're not using it, you're probably not thinking enough.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    36
    Thanks for the quick reply, guys. My next question is, do you have any references to a good tutorial on smart pointers? I've yet to learn these, and I must know the ins and outs .

  5. #5
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    RTFM. Sorry but smart pointers are all different. A good smart pointer class will behave a lot like a regular pointer though.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    If the reason for storing pointers instead of objects is that it avoids the copy, you should know that the new C++11 emplace methods can "move" the item into the vector rather than copying it in, so that there is no copy.
    There are less reasons to store pointers in a vector than ever before.
    Last edited by iMalc; 07-31-2012 at 12:59 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ Classes/Vectors help!
    By bigboybz in forum C++ Programming
    Replies: 8
    Last Post: 03-03-2011, 03:42 AM
  2. C++ Classes and Vectors help
    By bigboybz in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2011, 07:01 AM
  3. Classes or Vectors?
    By Ideswa in forum C++ Programming
    Replies: 4
    Last Post: 04-08-2006, 10:07 AM
  4. vectors and classes
    By jimothygu in forum C++ Programming
    Replies: 3
    Last Post: 04-27-2003, 07:53 PM
  5. vectors and classes
    By kooma in forum C++ Programming
    Replies: 3
    Last Post: 02-28-2002, 12:44 PM