Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This is a good reason to use boost::ptr_vector.</p> <p>It not only handles the fact that your objects need to be destroyed.<br> xtofl@: You forgot the virtual destructor.</p> <p>But it also makes the members look like objects by returning references rather than pointers. This allows you to use the standard algorithms much more naturally rather than playing around with pointers in your 'equal' function (which is very un C++ like).</p> <pre><code>#include &lt;boost/ptr_container/ptr_vector.hpp&gt; #include &lt;iostream&gt; class Shape { public: ~Shape() {} bool operator==(Shape const&amp; rhs) const { if (typeid(*this) != typeid(rhs)) { return false; } return this-&gt;isEqual(rhs); } private: virtual bool isEqual(Shape const&amp; rhs) const = 0; }; class Circle: public Shape { public: Circle(int r) :radius(r) {} private: virtual bool isEqual(Shape const&amp; r) const { Circle const&amp; rhs = dynamic_cast&lt;Circle const&amp;&gt;(r); return radius == rhs.radius; } int radius; }; class Rectangle: public Shape { public: Rectangle(int h,int w) :height(h) ,width(w) {} private: virtual bool isEqual(Shape const&amp; r) const { Rectangle const&amp; rhs = dynamic_cast&lt;Rectangle const&amp;&gt;(r); return (height == rhs.height) &amp;&amp; (width == rhs.width); } int height; int width; }; int main() { boost::ptr_vector&lt;Shape&gt; data; data.push_back(new Circle(5)); data.push_back(new Circle(6)); data.push_back(new Rectangle(7,4)); boost::ptr_vector&lt;Shape&gt;::iterator f; f = find(data.begin(),data.end(),Circle(6)); std::cout &lt;&lt; "Find(" &lt;&lt; (f - data.begin() ) &lt;&lt; ")" &lt;&lt; std::endl; } </code></pre>
    singulars
    1. This table or related slice is empty.
    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.
    2. VO
      singulars
      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