关于在实践中使用shared_from_this()的问题
以下代码片段可在 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 :: create
,foo :: 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
sharone_from_this()
旨在从共享类本身(因此其名称)中使用,而不是由外部实体使用,该实体始终可以访问sharone_ptr
参加那个课。例如,您可以拥有一个
database
view
s的类database
,该 s必须保留database
: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 ashared_ptr
to that class.For example, you can have a class
Database
that hands outView
s, which have to keep a back-pointer to theDatabase
: