为什么enable_shared_from_this一定要公开继承?
Why enable_shared_from_this must be publicly inherited?
我很难理解为什么这段代码会抛出错误。
SO 上的所有其他解决方案都表示您需要有一个 shared_ptr 实例才能使用 shared_from_this(),而我有它!
但是尽管如此,代码仍然像所有其他问题所描述的那样崩溃。
这里是代码的简化版本
main.cpp
uint16_t port = 9876;
auto inout = cms::MultiTcpInOut::create(port);
inout->start_accept();
MultiTcpInOut.h
#ifndef MULTITCPINOUT_H
#define MULTITCPINOUT_H
#include <memory>
#include <utility>
namespace cms {
using namespace std;
class MultiTcpInOut : private enable_shared_from_this<MultiTcpInOut>
{
public:
static shared_ptr<MultiTcpInOut> create(uint16_t port)
{
return shared_ptr<MultiTcpInOut>(new MultiTcpInOut(port));
}
virtual ~MultiTcpInOut();
// start_accept() can not be called from the constructor because it uses shared_from_this(), and
// make_shared<>() haven't finished yet when it is called. Let the instantiating code call it
void start_accept();
protected:
MultiTcpInOut(uint16_t port);
};
}
#endif // MULTITCPINOUT_H
MultiTcpInOut.cpp
void MultiTcpInOut::start_accept()
{
LOG(INFO) << "Starting accept";
auto socket = make_shared<tcp::socket>(io_service);
auto shrd_this = shared_from_this();
acceptor->async_accept(*socket.get(), bind(&MultiTcpInOut::handle_accept, shrd_this, socket, std::placeholders::_1));
}
将 private enable_shared_from_this<MultiTcpInOut>
更改为 public enable_shared_from_this<MultiTcpInOut>
解决了问题。
它没有说它必须被继承 publicly (at least reading the reference),我不明白为什么它没有,因为我是从 class 方法调用它的!
那么,为什么一定要 public?
写的这么清楚,规范里都没提到吗?
帮助可能遇到我同样问题的人
ENABLE_SHARED_FROM_THIS 必须继承为 PUBLIC!!!
这来自 cppreference:'A common (until C++17)standard (since C++17) implementation for enable_shared_from_this is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr detect the presence of an enable_shared_from_this base and assign the newly created std::shared_ptr to the internally stored weak reference.' 也就是说,std::shared_ptr<>
需要对该字段的写入权限,而不仅仅是您的 class。
我很难理解为什么这段代码会抛出错误。 SO 上的所有其他解决方案都表示您需要有一个 shared_ptr 实例才能使用 shared_from_this(),而我有它! 但是尽管如此,代码仍然像所有其他问题所描述的那样崩溃。
这里是代码的简化版本
main.cpp
uint16_t port = 9876;
auto inout = cms::MultiTcpInOut::create(port);
inout->start_accept();
MultiTcpInOut.h
#ifndef MULTITCPINOUT_H
#define MULTITCPINOUT_H
#include <memory>
#include <utility>
namespace cms {
using namespace std;
class MultiTcpInOut : private enable_shared_from_this<MultiTcpInOut>
{
public:
static shared_ptr<MultiTcpInOut> create(uint16_t port)
{
return shared_ptr<MultiTcpInOut>(new MultiTcpInOut(port));
}
virtual ~MultiTcpInOut();
// start_accept() can not be called from the constructor because it uses shared_from_this(), and
// make_shared<>() haven't finished yet when it is called. Let the instantiating code call it
void start_accept();
protected:
MultiTcpInOut(uint16_t port);
};
}
#endif // MULTITCPINOUT_H
MultiTcpInOut.cpp
void MultiTcpInOut::start_accept()
{
LOG(INFO) << "Starting accept";
auto socket = make_shared<tcp::socket>(io_service);
auto shrd_this = shared_from_this();
acceptor->async_accept(*socket.get(), bind(&MultiTcpInOut::handle_accept, shrd_this, socket, std::placeholders::_1));
}
将 private enable_shared_from_this<MultiTcpInOut>
更改为 public enable_shared_from_this<MultiTcpInOut>
解决了问题。
它没有说它必须被继承 publicly (at least reading the reference),我不明白为什么它没有,因为我是从 class 方法调用它的!
那么,为什么一定要 public? 写的这么清楚,规范里都没提到吗?
帮助可能遇到我同样问题的人
ENABLE_SHARED_FROM_THIS 必须继承为 PUBLIC!!!
这来自 cppreference:'A common (until C++17)standard (since C++17) implementation for enable_shared_from_this is to hold a weak reference (such as std::weak_ptr) to this. The constructors of std::shared_ptr detect the presence of an enable_shared_from_this base and assign the newly created std::shared_ptr to the internally stored weak reference.' 也就是说,std::shared_ptr<>
需要对该字段的写入权限,而不仅仅是您的 class。