Ashvtosh Goel
Ashvtosh Goel

Reputation: 11

How to declare multiple deadline timer in class definition?

I have managed to declare one deadline timer in a class.

class myTimerService
{
public:
    boost::asio::io_service ioService;
    myTimerService():t0(ioService){}
private:
    boost::asio::deadline_timer t0;
};

so the construct is able to construct the t0. How do I modify it to have another deadline_timer t1. How would the construct look like for following?

class myTimerService
{
public:
    boost::asio::io_service ioService;
    myTimerService()-------????
private:
    boost::asio::deadline_timer t0;
    boost::asio::deadline_timer t1;
};

Upvotes: 1

Views: 107

Answers (1)

sehe
sehe

Reputation: 393467

As the commenter said, base & member initializer list is comma-separated:

#include <boost/asio.hpp>
class myTimerService {
  public:
    boost::asio::io_service ioService;
    myTimerService() : t0(ioService), t1(ioService) {}

  private:
    boost::asio::deadline_timer t0;
    boost::asio::deadline_timer t1;
};

However, I'd suggest NSMI:

#include <boost/asio.hpp>

class myTimerService {
    boost::asio::io_service     ioService;
    boost::asio::deadline_timer t0{ioService}, t1{ioService};
};

Upvotes: 0

Related Questions