Ask Your Question

Revision history [back]

Just use boost::bind to create a "virtual" callback function based on your "real" one. I haven't got the syntax for callbacks to objects in my head right now, but

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", 1, boost::bind(callback, _1, i));

would create 10 subscribers that are each tied to the function callback(msg_type msg, int i).

Just use boost::bind to create a "virtual" callback function based on your "real" one. I haven't got the syntax for callbacks to objects in my head right now, but

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", 1, boost::bind(callback, _1, i));

would create 10 subscribers that are each tied to the function callback(msg_type msg, int i).

Edit: Using member-function as objects is done the same way as you'd do it without boost::bind.

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", 1, boost::bind(&callback, this, _1, i));

Just use boost::bind to create a "virtual" callback function based on your "real" one. I haven't got the syntax for callbacks to objects in my head right now, but

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", 1, boost::bind(callback, _1, i));

would create 10 subscribers that are each tied to the function callback(msg_type msg, int i).

Edit: Using member-function as objects is done the same way as you'd do it without boost::bind.

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", 1, boost::bind(&callback, boost::bind(&MyClass::callback, this, _1, i));

Just use boost::bind to create a "virtual" callback function based on your "real" one. I haven't got the syntax for callbacks to objects in my head right now, but

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", 1, boost::bind(callback, _1, i));

would create 10 subscribers that are each tied to the function callback(msg_type msg, int i).

Edit: Using a member-function as objects of an actual object is done the same way as you'd do it without boost::bind.

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", 1, boost::bind(&MyClass::callback, this, _1, i));
click to hide/show revision 5
No.5 Revision

Just use boost::bind to create a "virtual" callback function based on your "real" one. I haven't got the syntax for callbacks to objects in my head right now, but

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", node.subscribe(sub_name, 1, boost::bind(callback, _1, i));

would create 10 subscribers that are each tied to the function callback(msg_type msg, int i).

Edit: Using a member-function of an actual object is done the same way as you'd do it without boost::bind.

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe("sub_name", node.subscribe(sub_name, 1, boost::bind(&MyClass::callback, this, _1, i));

Just use boost::bind to create a "virtual" callback function based on your "real" one. I haven't got the syntax for callbacks to objects in my head right now, but

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe(sub_name, 1, boost::bind(callback, _1, i));

would create 10 subscribers that are each tied to the function callback(msg_type msg, int i).

Edit: Using a member-function of an actual object is done the same way as you'd do it without boost::bind.

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe(sub_name, 1, boost::bind(&MyClass::callback, this, _1, i));

Edit 2: With respect to the new question, the first thing I'd try is templating the subscriber to the correct datatype:

click to hide/show revision 7
Added templated version

Just use boost::bind to create a "virtual" callback function based on your "real" one. I haven't got the syntax for callbacks to objects in my head right now, but

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe(sub_name, 1, boost::bind(callback, _1, i));

would create 10 subscribers that are each tied to the function callback(msg_type msg, int i).

Edit: Using a member-function of an actual object is done the same way as you'd do it without boost::bind.

for (int i = 0; i < 10, i++)
     sub[i] = node.subscribe(sub_name, 1, boost::bind(&MyClass::callback, this, _1, i));

Edit 2: With respect to the new question, the first thing I'd try is templating the subscriber to the correct datatype:

[...]
sub[i] = node.subscribe<sensor_msgs::JointState>(sub_name, 1, boost::bind(&Myclass::callback, this, _1, i));
[...}