Note that there are some explanatory texts on larger screens.

plurals
  1. POExample to use shared_ptr?
    primarykey
    data
    text
    <p>Hi I asked a question today about <a href="https://stackoverflow.com/questions/3475030/different-types-of-objects-in-the-same-vector-array">How to insert different types of objects in the same vector array </a> and my code in that question was </p> <pre><code> gate* G[1000]; G[0] = new ANDgate() ; G[1] = new ORgate; //gate is a class inherited by ANDgate and ORgate classes class gate { ..... ...... virtual void Run() { //A virtual function } }; class ANDgate :public gate {..... ....... void Run() { //AND version of Run } }; class ORgate :public gate {..... ....... void Run() { //OR version of Run } }; //Running the simulator using overloading concept for(...;...;..) { G[i]-&gt;Run() ; //will run perfectly the right Run for the right Gate type } </code></pre> <p>and I wanted to use vectors so someone wrote that I should do that :</p> <pre><code>std::vector&lt;gate*&gt; G; G.push_back(new ANDgate); G.push_back(new ORgate); for(unsigned i=0;i&lt;G.size();++i) { G[i]-&gt;Run(); } </code></pre> <p>but then he and many others suggested that I would better use <a href="http://www.boost.org/doc/libs/1_43_0/libs/ptr_container/doc/guidelines.html" rel="noreferrer">Boost pointer containers<br> </a> or <code>shared_ptr</code>. I have spent the last 3 hours reading about this topic, but the documentation seems pretty advanced to me . ****Can anyone give me a small code example of <code>shared_ptr</code> usage and why they suggested using <code>shared_ptr</code>. Also are there other types like <code>ptr_vector</code>, <code>ptr_list</code> and <code>ptr_deque</code>** **</p> <p>Edit1: I have read a code example too that included:</p> <pre><code>typedef boost::shared_ptr&lt;Foo&gt; FooPtr; ....... int main() { std::vector&lt;FooPtr&gt; foo_vector; ........ FooPtr foo_ptr( new Foo( 2 ) ); foo_vector.push_back( foo_ptr ); ........... } </code></pre> <p>And I don't understand the syntax!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    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