#################################### ##FILL ME IN #################################### ## links to any required tutorials ## note.0= [[ROS/Tutorials|ROS Tutorials]] ## descriptive title for the tutorial ## title = Accessing Private Names from a NodeHandle ## multi-line description to be displayed in search ## description = This tutorial will show you how to access private [[Names]] with roscpp's `NodeHandle` API. ## the next tutorial description (optional) ## next = ## links to next tutorial (optional) ## next.0.link=[[roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks|Using Class Methods as Callbacks]] ## next.1.link= ## what level user is this tutorial for ## level= BeginnerCategory #################################### <> <> == Why not just ~name? == The introduction of `NodeHandles` in roscpp created something of a conundrum when dealing with private names. If I create a `NodeHandle` with its own namespace: {{{#!cplusplus ros::init(argc, argv, "my_node_name"); ros::NodeHandle nh("/my_node_handle_namespace"); }}} Where should a private name resolve? Some options would be: * `/my_node_handle_namespace/my_node_name/name` * `my_node_name/my_node_handle_namespace/name` * `/my_node_handle_namespace/name` * Something else entirely For this reason, `NodeHandle` does not allow passing private names directly to its methods, or to constructors that take a `NodeHandle` as an argument. == Accessing Private Names == The solution is to construct a `NodeHandle` with a private name as its namespace: {{{#!cplusplus ros::init(argc, argv, "my_node_name"); ros::NodeHandle nh1("~"); // must be in main() ros::NodeHandle nh2("~foo"); }}} `nh1`'s namespace is `/my_node_name`, and `nh2`'s namespace is `/my_node_name/foo`. So instead of doing this: {{{#!cplusplus ros::NodeHandle nh; nh.getParam("~name", ... ); }}} You do this: {{{#!cplusplus ros::NodeHandle nh("~"); nh.getParam("name", ... ); }}} '''Next Tutorial''': [[roscpp_tutorials/Tutorials/UsingClassMethodsAsCallbacks|Using Class Methods as Callbacks]] ## AUTOGENERATED DO NOT DELETE ## TutorialCategory ## FILL IN THE STACK TUTORIAL CATEGORY HERE