关于在实践中使用shared_from_this()的问题

发布于 2025-02-06 09:01:25 字数 2008 浏览 0 评论 0原文

以下代码片段可在 cppreference> cppReference

我很好奇最佳:: getPtr()是什么意图?我什么时候应该在实践中使用此方法?也许简单的演示代码有很大帮助。

struct Best : std::enable_shared_from_this<Best> // note: public inheritance
{
    std::shared_ptr<Best> getptr() {
        return shared_from_this();
    }
    // No public constructor, only a factory function,
    // so there's no way to have getptr return nullptr.
    [[nodiscard]] static std::shared_ptr<Best> create() {
        // Not using std::make_shared<Best> because the c'tor is private.
        return std::shared_ptr<Best>(new Best());
    }
private:
    Best() = default;
};

我写了一个简单的代码spippet 使用sharone_from_this()看不到任何有意义的东西。

如果我想创建一个新实例,我只会调用foo :: create()。如果我没有打电话给foo :: createfoo :: getptr()是毫无意义的(即根本无法调用它),因为还没有有效的实例。

如果我想念什么,请告诉我。

#include <iostream>
#include <memory>
 
class Foo : public std::enable_shared_from_this<Foo> {
private:     //the user should not construct an instance through the constructor below.                    
    Foo(int num):num_(num) { std::cout << "Foo::Foo\n"; }

public:
    ~Foo() { std::cout << "Foo::~Foo\n"; } 

    int DoSth(){std::cout << "hello world" << std::endl; return 0;}

    std::shared_ptr<Foo> getPtr() { return shared_from_this();}

    static std::shared_ptr<Foo> Create() {
        Foo* foo = new Foo(5);
        return std::shared_ptr<Foo>(foo);
    }

private:
    int num_;

};

int main()
{
    auto sp = Foo::Create();
    sp->DoSth();

    Foo& foo = *sp.get();
    auto sp1 = foo.getPtr();
    std::cout << sp.use_count() << std::endl;
}

The below code snippet is seen at cppreference.

I am curious about what the intention of Best::getptr() is? When should I use this method in practice? Maybe a simple demo code helps a lot.

struct Best : std::enable_shared_from_this<Best> // note: public inheritance
{
    std::shared_ptr<Best> getptr() {
        return shared_from_this();
    }
    // No public constructor, only a factory function,
    // so there's no way to have getptr return nullptr.
    [[nodiscard]] static std::shared_ptr<Best> create() {
        // Not using std::make_shared<Best> because the c'tor is private.
        return std::shared_ptr<Best>(new Best());
    }
private:
    Best() = default;
};

I wrote a simple code snippet to use shared_from_this(), but I still can't see anything meaningful.

If I want to create a new instance, I just call Foo::Create(). If I have not called Foo::Create, Foo::getPtr() is meaningless (i.e. it could not be invoked at all) since there is no valid instance yet.

If I miss something, please let me know.

#include <iostream>
#include <memory>
 
class Foo : public std::enable_shared_from_this<Foo> {
private:     //the user should not construct an instance through the constructor below.                    
    Foo(int num):num_(num) { std::cout << "Foo::Foo\n"; }

public:
    ~Foo() { std::cout << "Foo::~Foo\n"; } 

    int DoSth(){std::cout << "hello world" << std::endl; return 0;}

    std::shared_ptr<Foo> getPtr() { return shared_from_this();}

    static std::shared_ptr<Foo> Create() {
        Foo* foo = new Foo(5);
        return std::shared_ptr<Foo>(foo);
    }

private:
    int num_;

};

int main()
{
    auto sp = Foo::Create();
    sp->DoSth();

    Foo& foo = *sp.get();
    auto sp1 = foo.getPtr();
    std::cout << sp.use_count() << std::endl;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一场信仰旅途 2025-02-13 09:01:26

sharone_from_this()旨在从共享类本身(因此其名称)中使用,而不是由外部实体使用,该实体始终可以访问sharone_ptr参加那个课。

例如,您可以拥有一个database view s的类database,该 s必须保留database

struct View;

struct Database : enable_shared_from_this<Database>
{
    auto GetView() { return make_unique<View>(shared_from_this()); }
};

struct View
{
    shared_ptr<Database> db;
    View(shared_ptr<Database> db) : db(move(db)) {}
};

shared_from_this() is intended to be used from within the shared class itself (hence its name), not so much by an external entity, which can always have access to a shared_ptr to that class.

For example, you can have a class Database that hands out Views, which have to keep a back-pointer to the Database:

struct View;

struct Database : enable_shared_from_this<Database>
{
    auto GetView() { return make_unique<View>(shared_from_this()); }
};

struct View
{
    shared_ptr<Database> db;
    View(shared_ptr<Database> db) : db(move(db)) {}
};
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文