Base classes are initialised before members, so the io_service
is not created until after you try to dereference the uninitialised pointer to pass a reference to the base class.
But deriving from serial_port
seems rather odd; why not use aggregation instead? Make sure the service is declared before anything that uses it:
class myClass {
public:
myClass(std::string port) : serial_port(io_service, port) {}
// public interface to interact with the serial port and whatever else
private:
boost::asio::io_service io_service; // or a shared pointer if there's a good reason
boost::asio::serial_port serial_port;
};
It's also likely that you'd want to share one service with many objects, so perhaps it shouldn't be owned by this class at all:
class myClass {
public:
myClass(boost::asio::io_service & io_service, std::string port) :
serial_port(io_service, port)
{}
// public interface to interact with the serial port and whatever else
private:
boost::asio::serial_port serial_port;
};