ROS Resources: Documentation | Support | Discussion Forum | Index | Service Status | ros @ Robotics Stack Exchange
Ask Your Question

confusing about simple ros c++ code

asked 2013-05-31 17:18:20 -0500

AdrianPeng gravatar image

updated 2014-01-28 17:16:43 -0500

ngrennan gravatar image


I have a simple question about c++ pointer. This is the main.

int main(int argc, char** argv)
  ros::init(argc, argv, "joint_trajectory_action_node");
  ros::NodeHandle node;//("~");
  JointTrajectoryExecuter jte(node);


  return 0;

And this is part of the constructor of JointTrajectoryExecuter

  JointTrajectoryExecuter(ros::NodeHandle &n) :
    action_server_(node_, "joint_trajectory_action",
                   boost::bind(&JointTrajectoryExecuter::goalCB, this, _1),
                   boost::bind(&JointTrajectoryExecuter::cancelCB, this, _1),

node_ is defined as ros::NodeHandle node_;

I don't understand "node_(n)".

Can anyone give me any hint?


edit retag flag offensive close merge delete



I think you may be referring to a initialization list

mortonjt gravatar image mortonjt  ( 2013-05-31 18:09:27 -0500 )edit

1 Answer

Sort by ยป oldest newest most voted

answered 2013-05-31 19:30:58 -0500

If you look at the ros::NodeHandle documentation, you can find a copy constructor that matches that signature:

ros::NodeHandle::NodeHandle(const NodeHandle &rhs)

Essentially, this creates a new NodeHandle instance that inherits a copy of all the parameters from the original NodeHandle. In the JointTrajectoryExecuter class you reference, this creates a local NodeHandle variable within the class that is a a copy of the NodeHandle created when the node is first initialized. The class can then use this local copy for handling actions, messages, etc.

In general, this isn't necessary, as all NodeHandle objects in a given node point to the same underlying object. But this version does allow the new NodeHandle to inherit the namespace of the original NodeHandle, if one was specified.

edit flag offensive delete link more

Question Tools


Asked: 2013-05-31 17:18:20 -0500

Seen: 551 times

Last updated: May 31 '13