Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><strong><em>I'm told I have to use a vector of Object Pointers, not Objects. Why? My assignment calls for it specifically, but I'm also told it won't work otherwise.</em></strong></p> <p>Usually, one would avoid using vector of objects to avoid the problem of <strong><a href="https://stackoverflow.com/questions/274626/what-is-the-slicing-problem-in-c"><em>Object Slicing</em></a></strong>. To make polymorphism work You have to use some kind of pointers. I am not sure of how the classes in your assignment are aligned but probably you might have Inheritance there somewhere and hence if vector is storing objects of Base class and you insert objects of Derived class in it then it would cause the derived class members to slice off.</p> <p>The Best solution will be to use a smart pointer instead of a Raw pointer. The STL has an <code>auto_ptr</code>, but that cannot be used in a standard container.Boost smart pointers would be a best solution but as you already said you can't use Boost So in your case you can use your compiler's implementation of smart pointers, which comes in <code>TR1</code> namespace,remember though that there is some disagreement on the namespace for TR1 functions (Visual C++ puts them in <code>std::</code>, while GCC puts them in <code>std::tr1::</code>).</p> <p><strong><em>Where should I be creating this vector? Should it be part of my Package Class? How do I go about adding objects into it then?</em></strong><br> Your example code already has an example of adding a pointer to <code>Package</code> class in a vector. In a nutshell you will dynamically allocate pointers to <code>Package</code> and then add them to the vector.</p> <p><strong><em>Do I need a copy constructor? Why?</em></strong><br> The copy constructor generated by the compiler does member-wise copying. Sometimes that is not sufficient. For example:</p> <pre><code>class MyClass { public: MyClass( const char* str ); ~MyClass(); private: char* str; }; MyClass::MyClass( const char* str2 ) { str = new char[srtlen( str2 ) + 1 ]; strcpy( str, str2 ); } Class::~Class() { delete[] str; } </code></pre> <p>In this case member-wise copying of <code>str</code> member will not duplicate the buffer (only the pointer will be copied(<code>shallow copy</code>)), so the first to be destroyed copy sharing the buffer will call <code>delete[]</code> successfully and the second will run into <code>Undefined Behavior</code>. You need <code>deep copying copy constructor</code> (and assignment operator as well) in such a scenario. </p> <p>When to use a custom copy constructor is best defined by the <strong><em><a href="https://stackoverflow.com/questions/4172722/what-is-the-rule-of-three">Rule Of Three</a></em></strong>: </p> <pre><code>Whenever you are writing either one of Destructor, Copy Constructor or Copy Assignment Operator, you probably need to write the other two. </code></pre> <p><strong><em>What's the proper way to deconstruct my vector of object pointers?</em></strong><br> You will have to explicitly call delete on each contained pointer to delete the content it is pointing to.</p> <p><code>vector::erase</code><br> Removes from the vector container and calls its destructor but If the contained object is a pointer it doesnt take ownership of destroying it. </p> <p>Check out <a href="https://stackoverflow.com/questions/6353149/does-vectorerase-destroy-the-removed-object/6353169#6353169"><strong>this answer</strong></a> here to know how to corrctly delete a vector of pointer to objects.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload