class initialization problem

I have a Server class inherited from a Listener class, with the latter being bases on the boost::asio example code. My problem is that the Listener class requires am boost::asio::io_service & in order to initialize its member variables. I wanted the Server class to be responsible for starting a thread, owning the io_service, and running it. It seems I cannot do that! class Server : public Listener { public: Server() : Listener(ioService_) { } // Snip protected: boost::asio::io_service ioService_; }; class Listener { public: Listener(boost::asio::io_service & ioService) : socket_(ioService) { } // Snip protected: boot::asio::ip::tcp::socket socket_; }; The above throws an unhandled exception in the initialization of the derived class. I suppose this is because ioService_ is not constructed yet? Since the C++ rules, if I remember correctly, say the base class gets constructed before the members of the derived? So, I tried: Server() : ioService_(), Listener(ioService_) { } to try and force it, but it still throws an exception. The only way to get it to not throw an exception is to create the instance to io-service in main() and then pass it as a reference into the class constructor. If I do that, then there is no way for any class in my inheritance hierarchy to own the io_service. As such, I cannot start and stop it either. Is there any way to get ownership of the io_service in my class hierarchy?

Christopher Pisz wrote:
I have a Server class inherited from a Listener class, with the latter being bases on the boost::asio example code.
My problem is that the Listener class requires am boost::asio::io_service & in order to initialize its member variables.
I wanted the Server class to be responsible for starting a thread, owning the io_service, and running it.
It seems I cannot do that!
class Server : public Listener { public: Server() : Listener(ioService_) { }
// Snip
protected:
boost::asio::io_service ioService_; };
class Listener { public:
Listener(boost::asio::io_service & ioService) : socket_(ioService) { }
// Snip
protected: boot::asio::ip::tcp::socket socket_; };
The above throws an unhandled exception in the initialization of the derived class.
I suppose this is because ioService_ is not constructed yet? Since the C++ rules, if I remember correctly, say the base class gets constructed before the members of the derived?
So, I tried:
Server() : ioService_(), Listener(ioService_) { }
to try and force it, but it still throws an exception.
The only way to get it to not throw an exception is to create the instance to io-service in main() and then pass it as a reference into the class constructor. If I do that, then there is no way for any class in my inheritance hierarchy to own the io_service. As such, I cannot start and stop it either.
Is there any way to get ownership of the io_service in my class hierarchy?
class Server: protected boost::asio::io_service, public Listener Server (): Listener (*this) HTH, Chris

I have a Server class inherited from a Listener class, with the latter being bases on the boost::asio example code.
My problem is that the Listener class requires am boost::asio::io_service & in order to initialize its member variables.
I wanted the Server class to be responsible for starting a thread, owning the io_service, and running it.
It seems I cannot do that!
class Server : public Listener { public: Server() : Listener(ioService_) { }
// Snip
protected:
boost::asio::io_service ioService_; };
class Listener { public:
Listener(boost::asio::io_service & ioService) : socket_(ioService) { }
// Snip
protected: boot::asio::ip::tcp::socket socket_; };
The above throws an unhandled exception in the initialization of the derived class.
I suppose this is because ioService_ is not constructed yet? Since the C++ rules, if I remember correctly, say the base class gets constructed before the members of the derived?
So, I tried:
Server() : ioService_(), Listener(ioService_) { }
to try and force it, but it still throws an exception.
Initialization order does not depend on the order of objects in the initialization list, so you can't "force" anything this way. But you can move io_service definition to Listener, and then access it from Server.
participants (3)
-
Christopher Pisz
-
Igor R
-
Krzysztof Żelechowski