The class Request
adds itself into a class EventLoop
. The shared_ptr loop
is not nullptr. However, I get some problems. The code is as follows:
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
class Request;
typedef std::shared_ptr<Request> RequestPtr;
class EventLoop
{
public:
EventLoop();
~EventLoop();
vector<RequestPtr> relist;
void add_request(RequestPtr re)
{
relist.push_back(re);
}
};
typedef std::shared_ptr<EventLoop> EventLoopPtr;
EventLoop::EventLoop() {}
EventLoop::~EventLoop() {}
class Request: public std::enable_shared_from_this<Request>
{
public:
Request();
~Request();
EventLoopPtr loop;
void add_inself()
{
loop->add_request(shared_from_this());
cout << "Yes, success adding into it" << endl;
}
};
Request::Request(): loop(std::make_shared<EventLoop>())
{}
Request::~Request()
{}
int main()
{
Request r;
r.add_inself();
std::cout << "END" << std::endl;
return 0;
}
The error message is:
terminate called after throwing an instance of 'std::bad_weak_ptr'
what(): bad_weak_ptr
Aborted
Any solutions? Thank you very much
Aucun commentaire:
Enregistrer un commentaire