I have a class with an object as a member which doesn\'t have a default constructor. I\'d like to initialize this member in the constructor, but it seems that in C++ I can\'
You could turn the _sock
member into a smart pointer:
#include
#include
#include
using boost::asio::ip::udp;
template
class udp_sock
{
public:
udp_sock(std::string host, unsigned short port);
private:
boost::asio::io_service _io_service;
boost::scoped_ptr _sock_ptr;
boost::array _buf;
};
template
udp_sock::udp_sock(std::string host = "localhost",
unsigned short port = 50000)
{
udp::resolver res(_io_service);
udp::resolver::query query(udp::v4(), host, "spec");
udp::endpoint ep = *res.resolve(query);
ep.port(port);
_sock_ptr.reset(new udp::socket(_io_service, ep));
}