Note that there are some explanatory texts on larger screens.

plurals
  1. POshared_ptrs and the rule of three
    primarykey
    data
    text
    <p>I am continuing my process of understanding these topics, and I wanted to try to actually use my knowledge on a class that I already had created previously called <code>ArrayList</code>, which dynamically allocates memory when the original array created when the class is created gets filled up. This is what (part of) my class looks like: [I guess most of the code itself isn't important, I'm just showing what I have. The main point of the question is after all the code]</p> <p>And here are the functions that take care of adding elements to the array and reallocating memory when it gets filled up. The append function might look at bit weird because I'm trying to place the elements in ascending order, but that part's not really important.</p> <p>Now that I've posted that class that took a lot more space than expected, here is my question:</p> <p>How exactly would I use shared_ptrs with this class? I know that shared_ptrs make it easier to manage my dynamically allocated resources, but would a shared_ptr in this case be used to create the original array, and to delete the old array automatically when the pointer to the array is assigned to the new array? I can see how I would do that, but how would that have any effect on the rule of three [which I read basically is not needed if you use shared_ptrs]. I almost feel like..there are two dimensions to using shared_ptr in this case; one used to actually take care of allocating and deallocating resources for the object itself, but another also if I wanted to create a new object and copy the original object's array into the second object...if this makes sense? I'm guessing to do that I would have</p> <pre><code>ArrayList AL(10); shared_ptr&lt;ArrayList&gt; ptr = make_shared&lt;ArrayList&gt;(AL); </code></pre> <p>Is this effectively doing the same thing as if I had done</p> <pre><code>ArrayList AL(10); ArrayList AL2(AL); </code></pre> <p>except that it does the job of the destructor, copy constructor, and copy-assignment operator by itself? Sorry if this was all confusing and long but I'm just...confused.</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.
    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