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
A reason to prefer the second one is when you declare multiple variables at once:
string *str, *foo;
string* str, foo;
Those two lines are different, the first one declares to pointers, whereas the second one declares one pointer to string
and one string
.
[Comment by FredOverflow] This problem can be solved by some template magic, though ;-)
template <typename T>
struct multiple
{
typedef T variables;
};
multiple<string*>::variables str, foo;
Although I prefer the first one there is a reason to prefer the second, consider:
string* str, a;
string *str, a;
In the last case it is clear that *
applies only to str
. However using such declarations is often considered a bad style.
I do it like this:
string *str;
Because it makes a difference when you do this
string *str, *str2;
You could also do
typedef string* stringPtr;
So that you could do
stringPtr str, str2;
In C++ - neither - this should be const string& str
.
Or is that const string &str
? Hmm.