Note that there are some explanatory texts on larger screens.

plurals
  1. POC++11 Convert traditional pointer to smart pointer. Using up-to-date SVN Clang and libc++ from llvm
    text
    copied!<p>I've got a function inside a class (A) that essentially takes as a parameter a pointer to another class (B). The B class is inherited by multiple other classes which it should also accept.</p> <p>What I would like to do is take ownership of this pointer to store for later use in the class, it won't be used outside the class again for anything else. While I would make the parameter a shared_ptr, I would like to avoid that as much as possible due to other people I work with who don't quite get the whole smart pointer thing. Is there any way to do this? </p> <p>Here's a sort of example that I would like to do, although from what I've tested this doesn't work.</p> <pre><code>//In .h file std::vector&lt; unique_ptr&lt; B &gt; &gt; data_store; //In .cpp file void A::add_data(B* NewItem) { data_store.resize(data_store.size()+1); data_store[data_store.size()-1] = NewItem; } </code></pre> <p>Second to that, I would like to maybe use a copy constructor or something similar to use a smart pointer inside the class: with what I'm having to do it could get a bit ugly if I have to do manual deletes. The problem with that is that I don't know whether it's the base class (B) coming in or whether it's a class that inherited from B. I'm not too sure how to deal with that without having to hard-code in some kind of checkable ID for the class and using the correct copy/move constructors, which I would like to avoid at all costs.</p> <p>I'm using an updated Clang and libC++ from llvm which I updated about 10am UK time on 12th March 2012.</p>
 

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