将其转变为std ::shared_ptr

问题描述 投票:0回答:3
我试图将

this

铸造到std :: shared_ptr,我可以将其传递到构造函数中。请注意,此代码位于ShockGame的构造函数中。 我收到错误:
no matching function for call to 'SE::ShockRenderSystem::ShockRenderSystem(std::shared_ptr<SE::ShockGame>*, sf::VideoMode&)' candidate is: SE::ShockRenderSystem::ShockRenderSystem(std::shared_ptr<ShockGame>, sf::VideoMode)

thanks对任何帮助!


    

来自

ShockGame

的derive

std::enable_shared_from_this
c++ pointers memory this smart-pointers
3个回答
4
投票

对于任何来到此线程的人,请记住用
this替换您的
shared_from_this()
指针。


0
投票
std::enable_shared_from_this

shared_from_this()
的使用,但它们的用法有些怪异。这是一个例子,可能会有所帮助。
#include <memory>

//The class must inherit from std::enable_shared_from_this.
template <typename K, typename V>
struct TreeNode : public std::enable_shared_from_this<TreeNode<K, V>>
{
    std::shared_ptr<TreeNode<K, V>> find(K key) {
        //Must call shared_from_this() using this->.
        std::shared_ptr<TreeNode<K, V>> root = this->shared_from_this();
        //...
    }
}


0
投票
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.