title: C++ Boost ASIO: initializing io_context inside class:
body: <p>I am following Boost UDP multicast sender tutorial <a href="http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/example/cpp11/multicast/sender.cpp" rel="nofollow noreferrer">here</a>
  .  I modify it to make a class as follow:</p>
  
  <pre><code>#define _CRT_SECURE_NO_WARNINGS
  #include &lt;ctime&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;boost/array.hpp&gt;
  #include &lt;boost/bind.hpp&gt;
  #include &lt;boost/shared_ptr.hpp&gt;
  #include &lt;boost/asio.hpp&gt;
  #include &lt;boost/lexical_cast.hpp&gt;
  #include &lt;boost/thread.hpp&gt;
  using boost::asio::ip::udp;
  using std::cout;
  using std::cin;
  using std::endl;
  
  class sender
  {
  private:
      boost::asio::io_context io_context;
      boost::asio::ip::udp::endpoint endpoint_;
      boost::asio::ip::udp::socket socket_;
      int message_count_;
      std::string message_;
      bool showBroadcast;
  
  public:
      // constructor
      sender(std::string multicast_address, unsigned short multicast_port, bool show = true)
      {
          boost::asio::io_context io_context;
          boost::asio::ip::udp::endpoint endpoint_(boost::asio::ip::make_address("192.168.0.255"), 13000);
          boost::asio::ip::udp::socket socket_(io_context, endpoint_.protocol());
          socket_.set_option(boost::asio::ip::udp::socket::reuse_address(true));  // no need
      }
  
      // destructor
      ~sender()
      {
          cout &lt;&lt; "UDP sender exiting." &lt;&lt; endl;
      }
  
  private:
      std::string get_input()
      {
          std::string result;
          cout &lt;&lt; "Enter your message: ";
          getline(cin, result);
          return result;
      }
  
      std::string make_daytime_string()
      {
          using namespace std; // For time_t, time and ctime;
          time_t now = time(0);
          std::string result = ctime(&amp;now);
          return result.erase(result.length() - 1, 1);
      }
  
      std::string some_string()
      {
          std::string result;
          result = make_daytime_string();
          return result;
      }
  };
  
  int main(int argc, char* argv[])
  {
      try
      {
          sender s("192.168.0.255", 13000);
      }
      catch (std::exception&amp; e)
      {
          std::cerr &lt;&lt; "Exception: " &lt;&lt; e.what() &lt;&lt; "\n";
      }
  
      return 0;
  }
  </code></pre>
  
  <p>I wish to encapsulate io_context object inside the class, rather than having it outside.  VC++ complains:</p>
  
  <p>boost::asio::basic_datagram_socket': no appropriate default constructor available</p>
  
  <p>I believe it is trying to force me to have the constructor as follow (which I try to move away from):</p>
  
  <pre><code>    sender(boost::asio::io_context&amp; io_context, const boost::asio::ip::address&amp; multicast_address, unsigned short multicast_port, bool show = true) 
      : endpoint_(multicast_address, multicast_port),
      socket_(io_context, endpoint_.protocol())
  </code></pre>
  
  <p>How can I possibly have everything encapsulate inside my class?  Why does Boost force me to do the other way?  Please help.  Thank you so much.</p>
  
  <p>This seems to be due to io_context being non-copyable as suggested <a href="https://stackoverflow.com/questions/29623652/using-boostasioio-service-as-class-member-field">here</a>
  .  I wish to have this class copyable.  Any idea?</p>