Reputation: 12858
Sorry, I'm a C++ novice. I was looking through some code and ran across this line of code:
string cmd(*iter);
Obviously the variable "cmd" is being defined as a string, but the part that I don't understand is the bit inside the parenthesis: (*iter)
I know the "*iter" is a pointer but what does it mean to have a variable declaration followed by parenthesis?
Sorry for such a dumb question.
Upvotes: 0
Views: 98
Reputation: 320361
In "classic" C++ language you have two forms of initialization syntax:
copy-initialization
int i = 5;
and direct-initialization
int i(5);
They are not always exactly the same, but for basic intents and purposes they do the same thing. (C++11 further extended the variety, but I won't go into that here.) In my above examples they are actually exactly the same: variable i
will be initialized with 5
in both cases.
So your
string cmd(*iter);
has the same effect as
string cmd = *iter;
i.e. it initializes string cmd
with value of *iter
, where iter
is probably an iterator of some kind.
Assuming string
is actually the std::string
class from Standard Library, your string cmd(*iter)
will invoke one of std::string
s constructors. Which one - depends on the type of *iter
.
Upvotes: 3
Reputation: 41
initialize string cmd via *iter
.
NOTE: iter does not necessarily have to be a pointer, it could also be an iterator.
You should check the iterator's *
operator.
Upvotes: 3
Reputation: 17327
It calls the constructor of string
with the value pointed by iter
. This line of code simply creates a new string
instance called cmd
and initializes to the value of the iterator.
Upvotes: 1
Reputation: 2612
It is calling the constructor of the string class which accepts this kind of parameter. The same class can have different constructors.
Upvotes: 1