Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you want to do is creating a <code>shared_ptr</code> from formerly unmanaged memory. <code>dynamic_pointer_cast</code> isn't the tool for the job. Instead this should work:</p> <pre><code>resources.insert(std::pair&lt;const std::string, std::shared_ptr&lt;sf::Texture&gt;&gt;("1", std::shared_ptr&lt;sf::Texture&gt;(new sf::Texture(tempResource))); //or: resources.insert(std::make_pair("1", std::shared_ptr&lt;sf::Texture&gt;(new sf::Texture(tempResource))); </code></pre> <p>However constructing a <code>shared_ptr</code> from newly allocated memory is somewhat wasterful. since this does two allocations (one for the memory and one for the internal data of the <code>shared_ptr</code>). If you use <code>std::make_shared(tempResource)</code> those two allocations can be combined into one for higher efficiency.</p> <p>C++11 would also allow <code>resources.emplace("1", std::make_shared(tempResource)</code> for inplace construction of the pair, but last time I checked the compiler support for that was somewhat lacking.</p> <p>As a side note: if you don't need <code>tempResource</code> anymore, you might want to consider moving (instead of copying it into the datastructure by using <code>std::move(tempResource)</code> instead of <code>tempResource</code> for constructiong the newly allocated object. Depending on the implementation of <code>sf::texture</code> this might (or might not) be more efficient. If the object isn't truely move-constructible (so move-construction is basically the same as a copy), you might want to constructing the <code>Texture</code> directly into a <code>shared_ptr</code> and calling load on that one:</p> <pre><code>auto tempResource = std::make_shared&lt;sf::Texture&gt;(); tempResource-&gt;loadFromFile("1.PNG"); resources.emplace("1",std::move(tempResource));//or insert if emplace isn't availible </code></pre>
    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.
 

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