Which one is the best way to write:
string* str
Or:
string *str
Is there any drawback of side effect to one of them ?
Thanks
Which one is the best way to write:
string* str
Or:
string *str
Is there any drawback of side effect to one of them ?
Thanks
In C++ - neither - this should be
const string& str
.Or is that
const string &str
? Hmm.Although I prefer the first one there is a reason to prefer the second, consider:
In the last case it is clear that
*
applies only tostr
. However using such declarations is often considered a bad style.A reason to prefer the second one is when you declare multiple variables at once:
Those two lines are different, the first one declares to pointers, whereas the second one declares one pointer to
string
and onestring
.[Comment by FredOverflow] This problem can be solved by some template magic, though ;-)
I do it like this:
Because it makes a difference when you do this
You could also do
So that you could do