Initialize Boost shared_ptr in the constructor

I have a class that has a boost::asio::io_service . I want this object to be stored in boost::shared_ptr .

So, my title looks like this (I got rid of any unnecessary code so that it does not distract)

 class CommandDispatcher { private: boost::shared_ptr<boost::asio::io_service> m_ioservice; public: CommandDispatcher(); }; 

When I create the CommandDispatcher object, I want the io_service object to io_service initialized for the pointer. Now I'm not quite sure how to do this. I looked at two different questions, but only one works, and I'm not quite sure if it is good. But see for yourself:

 CommandDispatcher::CommandDispatcher() { m_ioservice.reset(new boost::asio::io_service); // this actually works //m_ioservice = boost::make_shared<boost::asio::io_service> // (new boost::asio::io_service); // this doesn't work } 

So, the reset call works, but I think it really does reassign the pointer. Therefore, you should not use it, but it does not seem like the best solution for me. A suggestion for calling make_shared , which I found in another question. But that just won't work for me (I implemented it as described in the official promotion example). I get

/usr/local/include/boost/smart_ptr/make_shared.hpp:189: error: invalid conversion from 'boost::asio::io_service*' to 'size_t'

/usr/local/include/boost/smart_ptr/make_shared.hpp:189: error: initializing argument 1 of 'boost::asio::io_service::io_service(size_t)'

I'm not quite sure how to do it now, which would be best (maybe there is another option). Or maybe I'm doing it right, but I am getting something with an io_service error.

I hope this question was no longer here (I raised some old question, but the answer was not like me).

+6
source share
4 answers
 CommandDispatcher::CommandDispatcher() : m_ioservice(new boost::asio::io_service) // ver 1. this is how you should do it. { //m_ioservice.reset(new boost::asio::io_service); // ver 2 //m_ioservice = boost::make_shared<boost::asio::io_service>(); // ver 3 } 
+7
source

If you use make_shared , you are not using new yourself; you pass the constructor arguments to it, and it will create an object for you. There are no arguments in this case, so it's simple:

 m_ioservice = boost::make_shared<boost::asio::io_service>(); 

although it would be better to initialize it in the initialization list, and not in the constructor body:

 CommandDispatcher::CommandDispatcher() : m_ioservice(boost::make_shared<boost::asio::io_service>()) { } 

Using make_shared has the advantage that it will only perform one memory allocation, while initialization using new boost::asio::io_service will require two (one for the object and one for the total number of links).

+8
source

Good way probably

 CommandDispatcher::CommandDispatcher() : m_ioservice(new boost::asio::io_service) { } 

because instead, you create shared_ptr by default and then reassign it.

Or, equivalently, using make_shared :

 CommandDispatcher::CommandDispatcher() : m_ioservice(boost::make_shared<boost::asio::io_service>()) { } 
+1
source

There are several ways:

  • for easy initialization, create in the list of constructors:

.

 CommandDispatcher::CommandDispatcher() : m_ioservice( new boost::asio::io_service ) { } 
  • for dependency injection using factory:

.

 CommandDispatcher::CommandDispatcher() : m_ioservice( Factory::Create() ) { } 
  • to inject dependencies by using an already created object:

.

 CommandDispatcher::CommandDispatcher( boost::shared_ptr< boost::asio::io_service > service ) : m_ioservice( service ) { } 
0
source

Source: https://habr.com/ru/post/907772/


All Articles