Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here</p> <pre><code>i-&gt;parent = pBase(this); </code></pre> <p>you create a smart pointer from a plain old pointer to an object which you didn't get directly from new. Never do this.</p> <p>As @Roddy explained, you get separate smart pointer objects, with separate reference counters. Two reference counters for one pointee won't work.</p> <p>In your case, it's probably ok to make parent a normal pointer, as @Roddy proposed. This way, you don't get in trouble with cyclic references. Just be sure that you never access the parent pointer after you deleted the parent. No problem if you delete all the children together with the parent (which happens automatically, unless you store more smart pointers to them, somewhere else)</p> <p>If you want to initialize a smart pointer, you've got two choices, basically: Use smart pointers in every interface. Unfortunately that doesn't work for "this" because that's an implicit parameter. You would need to pass the smart pointer, which you already created, to the method manually, in an extra parameter. Like this:</p> <pre><code>tree-&gt;add(tree, child); </code></pre> <p>Which is kind of ugly, so you may want to consider making "add" a static method, so you won't need to pass the parent twice.</p> <p>The other choice: Use another kind of smart pointer, like boost::intrusive_ptr, where you can store the reference count in the pointee. This way, you are able to find the reference count, even if you've got only a dumb pointer like "this".</p> <p>Edit: The answer by @jpalecek, below, is better. Use that one. Sebastian.</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.
 

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