May 2, 2018 at 7:41pm May 2, 2018 at 7:41pm UTC
A.
test::NodeHandle private_nh
//creating a class
Driver::Driver(): private_nh("~"),
frame_id(-1),
what does ("~") in the above line of code.
B.
int32_t frame_id;
frame_id(-1)
what does (-1) in the above line of code. should not the line be,
frame_id = -1
May 2, 2018 at 8:06pm May 2, 2018 at 8:06pm UTC
"~" is a string with a single character (~) in it. It is initializing a string variable like the -1 below.
the (-1) is correct in A. It is doing exactly that (frame_id is -1) but the syntax for initialization lists is a little different.
Both things in A are doing the same thing, just different variable types.
B should be as you said. Its legal and it works, but using () as a statement is cryptic and of no value here.
Last edited on May 2, 2018 at 8:18pm May 2, 2018 at 8:18pm UTC
May 2, 2018 at 9:07pm May 2, 2018 at 9:07pm UTC
Suppose
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
class Driver
{
public :
Driver(string home="~" ) :
frame_id_(-1),
home_directory_(home)
{
normal_home_ = home_directory_=="~" ;
}
private :
int frame_id_;
string home_directory_;
bool normal_home_;
};
Variables in the initializer list are evaluated first, and constructor body (e.g. normal_home_) only afterwards. For context, "~" might be the home directory on linux-like systems.
Last edited on May 2, 2018 at 9:10pm May 2, 2018 at 9:10pm UTC