Pointer to vector vs vector of pointers vs pointer to vector of pointers

Just wondering what you think is the best practice regarding vectors in C++.

If I have a class containing a vector member variable. When should this vector be declared a:

  • "Whole-object" vector member varaiable containing values, ie vector<MyClass> my_vector;
  • Pointer to a vector, ie vector<MyClass>* my_vector;
  • Vector of pointers, ie vector<MyClass*> my_vector;
  • Pointer to vector of pointers, ie vector<MyClass*>* my_vector;
  • I have a specific example in one of my classes where I have currently declared a vector as case 4, ie vector<AnotherClass*>* my_vector; where AnotherClass is another of the classes I have created.

    Then, in the initialization list of my constructor, I create the vector using new:

    MyClass::MyClass()
    : my_vector(new vector<AnotherClass*>())
    {}
    

    In my destructor I do the following:

    MyClass::~MyClass()
    {
      for (int i=my_vector->size(); i>0; i--)
      {
        delete my_vector->at(i-1);
      }
      delete my_vector;
    }
    

    The elements of the vectors are added in one of the methods of my class. I cannot know how many objects will be added to my vector in advance. That is decided when the code executes, based on parsing an xml-file.

    Is this good practice? Or should the vector instead be declared as one of the other cases 1, 2 or 3 ?

    When to use which case?

    I know the elements of a vector should be pointers if they are subclasses of another class (polymorphism). But should pointers be used in any other cases ?

    Thank you very much!!


    Usually solution 1 is what you want since it's the simplest in C++: you don't have to take care of managing the memory, C++ does all that for you (for example you wouldn't need to provide any destructor then).

    There are specific cases where this doesn't work (most notably when working with polymorphous objects) but in general this is the only good way.

    Even when working with polymorphous objects or when you need heap allocated objects (for whatever reason) raw pointers are almost never a good idea. Instead, use a smart pointer or container of smart pointers. Modern C++ compilers provide shared_ptr from the upcoming C++ standard. If you're using a compiler that doesn't yet have that, you can use the implementation from Boost.


    Definitely the first!

    You use vector for its automatic memory management. Using a raw pointer to a vector means you don't get automatic memory management anymore, which does not make sense.

    As for the value type: all containers basically assume value-like semantics. Again, you'd have to do memory management when using pointers, and it's vector's purpose to do that for you. This is also described in item 79 from the book C++ Coding Standards. If you need to use shared ownership or "weak" links, use the appropriate smart pointer instead.


    Deleting all elements in a vector manually is an anti-pattern and violates the RAII idiom in C++. So if you have to store pointers to objects in a vector , better use a 'smart pointer' (for example boost::shared_ptr ) to facilitate resource destructions. boost::shared_ptr for example calls delete automatically when the last reference to an object is destroyed.

    There is also no need to allocate MyClass::my_vector using new . A simple solution would be:

    class MyClass {
    
       std::vector<whatever> m_vector;
    };
    

    Assuming whatever is a smart pointer type, there is no extra work to be done. That's it, all resources are automatically destroyed when the lifetime of a MyClass instance ends.

    In many cases you can even use a plain std::vector<MyClass> - that's when the objects in the vector are safe to copy.

    链接地址: http://www.djcxy.com/p/31650.html

    上一篇: 指针和指针变量有什么区别?

    下一篇: 指向矢量vs指针向量的指针vs指向矢量的指针