Like Tree1Likes
  • 1 Post By laserlight

vectors of 'new' classes

This is a discussion on vectors of 'new' classes within the C++ Programming forums, part of the General Programming Boards category; OK suppose I had a code like this Code: vector<base *> vec; vec.push_back(new derived()); delete vec[0]; Code like this does ...

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

    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
    21,758
    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.
    Elysia likes this.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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
    35
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,709
    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,305
    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, 02:42 AM
  2. C++ Classes and Vectors help
    By bigboybz in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2011, 06: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, 11:44 AM

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