Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you have here might not crash, but it probably won't do what you want either! Zeroing out the vector won't call the destructor for each <code>a_class</code> instance. It will also overwrite the internal data for <code>a_class.even_more_values</code> (so if your <code>push_back()</code> is after the <code>memset()</code> you are likely to get an access violation).</p> <p>I would do two things differently:</p> <ol> <li>Use std::vector for your storage both in <code>a_class</code> and in <code>some_function()</code>.</li> <li>Write a destructor for <code>a_class</code> that cleans up properly</li> </ol> <p>If you do this, the storage will be managed for you by the compiler automatically.</p> <p>For instance:</p> <pre><code>class a_class { public: a_class() {...} ~a_class() { /* make sure that even_more_values gets cleaned up properly */ } int some_method(int some_param) {...} int value_1; int value_2; float value_3; std::vector&lt;some_other_class*&gt; even_more_values; /* and so on */ } void some_function() { std::vector&lt;a_class&gt; a_instances( 10 ); // Pass a_instances into these functions by reference rather than by using // a global. This is re-entrant and more likely to be thread-safe. do_stuff( a_instances ); do_more_stuff( a_instances ); // a_instances will be cleaned up automatically here. This also allows you some // weak exception safety. } </code></pre> <p>Remember that if <code>even_more_values</code> contains <em>pointers</em> to other objects, you will need to delete those objects in the destructor of <code>a_class</code>. If possible, <code>even_more_values</code> should contain the objects themselves rather than pointers to those objects (that way you may not have to write a destructor for <code>a_class</code>, the one the compiler provides for you may be sufficient).</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.
    2. 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