Note that there are some explanatory texts on larger screens.

plurals
  1. POThou shalt not inherit from std::vector
    primarykey
    data
    text
    <p>Ok, this is really difficult to confess, but I do have a strong temptation at the moment to inherit from <code>std::vector</code>. </p> <p>I need about 10 customized algorithms for vector and I want them to be directly members of the vector. But naturally I want also to have the rest of <code>std::vector</code>'s interface. Well, my first idea, as a law-abiding citizen, was to have an <code>std::vector</code> member in <code>MyVector</code> class. But then I would have to manually reprovide all of the std::vector's interface. Too much to type. Next, I thought about private inheritance, so that instead of reproviding methods I would write a bunch of <code>using std::vector::member</code>'s in the public section. This is tedious too actually. </p> <p>And here I am, I really do think that I can simply inherit publicly from <code>std::vector</code>, but provide a warning in the documentation that this class should not be used polymorphically. I think most developers are competent enough to understand that this shouldn't be used polymorphically anyway.</p> <p>Is my decision absolutely unjustifiable? If so, why? Can you provide an alternative which would have the additional members <strong>actually</strong> members but would not involve retyping all of vector's interface? I doubt it, but if you can, I'll just be happy.</p> <p>Also, apart from the fact that some idiot can write something like</p> <pre><code>std::vector&lt;int&gt;* p = new MyVector </code></pre> <p>is there any other <strong><em>realistic</em></strong> peril in using MyVector? By saying realistic I discard things like imagine a function which takes a pointer to vector ...</p> <p>Well, I've stated my case. I have sinned. Now it's up to you to forgive me or not :)</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.
 

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