Note that there are some explanatory texts on larger screens.

plurals
  1. POboost::shared_ptr and Inheritance
    primarykey
    data
    text
    <p>I am facing a situation in which I have a <code>std::vector</code> of <code>boost::shared_ptr</code>s of a base class. During the course of my program I need to store shared pointers to derived class objects in that vector too and at some time later in the program, need to retrieve those shared pointers.</p> <p>Following code illustrates my problem:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; using namespace std; #include &lt;boost/make_shared.hpp&gt; #include &lt;boost/foreach.hpp&gt; class Base { public: virtual ~Base() { } }; /******************************************/ typedef boost::shared_ptr&lt; Base &gt; BasePtr; /******************************************/ class Derived1 : public Base { public: void derived1_test() { cout &lt;&lt; "derived1_test" &lt;&lt; endl; } /******************************************/ int i1; }; /******************************************/ typedef boost::shared_ptr&lt; Derived1 &gt; Derived1Ptr; /******************************************/ class Derived2 : public Base { public: void derived2_test() { cout &lt;&lt; "derived2_test" &lt;&lt; endl; } /******************************************/ int i2; }; /******************************************/ typedef boost::shared_ptr&lt; Derived2 &gt; Derived2Ptr; /******************************************/ int main() { Derived1Ptr d1 = boost::make_shared&lt; Derived1 &gt;(); Derived2Ptr d2 = boost::make_shared&lt; Derived2 &gt;(); vector&lt; BasePtr &gt; v; v.push_back( d1 ); v.push_back( d2 ); BOOST_FOREACH(BasePtr bPtr, v) { try { Derived1&amp; d11 = dynamic_cast&lt; Derived1&amp; &gt;( *bPtr ); d11.derived1_test(); } catch (const std::bad_cast&amp; e) { Derived2&amp; d22 = dynamic_cast&lt; Derived2&amp; &gt;( *bPtr ); d22.derived2_test(); } } return 0; } </code></pre> <p>In the above code, if I change the code in <code>BOOST_FOREACH</code> from</p> <pre><code>Derived1&amp; d11 = dynamic_cast&lt; Derived1&amp; &gt;( *bPtr ); </code></pre> <p>to</p> <pre><code>Derived1Ptr d11 = dynamic_cast&lt; Derived1Ptr &gt;( bPtr ); </code></pre> <p>, I get following compile time error on VS2010</p> <pre><code>invalid target type for dynamic_cast target type must be a pointer or reference to a defined class </code></pre> <p>My problem is that I want to work with <code>boost::shared_ptr</code> not references. Secondly, I am using <code>dynamic_cast</code> which will throw a <code>std::bad_cast</code> exception when object reference is of a different type (tried using it with shared pointers but get the compiler error mentioned previously). That is clearly very slow. I want to be able to use a more performance oriented approach. What I am looking for here is any solution instead of using <code>dynamic_cast</code> and exception handling.</p> <p>Any suggestion with regards to code or change in design are welcome.</p>
    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.
 

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