user3286661 user3286661 - 1 year ago 136
C++ Question

Member initialization list vs assignment/copy constructor (in boost deadline_timer)

I have the following class declaration:

class A {
boost::asio::io_service io;
boost::asio::deadline_timer t;

The following constructor for class
works fine:

A::A() : t(io) {
// do stuff

But when I write this:

A::A() {
// do stuff

I get the following error:

error: no match for call to ‘(boost::asio::deadline_timer {aka

Perhaps because the copy constructor for
is not defined. But what is happening in case of member initialization list? Does it not use the copy constructor?

My question perhaps can be made more general in regard to what is the mechanism used in member initialization lists vs when we use assignment/copy constructor inside the class constructor.

Answer Source

The issue has nothing to do with copy constructor, and you didn't call it at all. The point is the two t(io)s are not the same thing.

t(io) in member initialization list means construct t by the constructor taking io as its argument. (I suppose boost::asio::deadline_timer has a constructor taking boost::asio::io_service as parameter.)

t(io) in the body of constructor is a statement, means calling t as a functor, passing io as its argument. It fails because boost::asio::deadline_timer doesn't support such functor behavior.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download