0

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

Michael Petrotta
  • 59,888
  • 27
  • 145
  • 179
Matthieu Napoli
  • 48,448
  • 45
  • 173
  • 261
  • 5
    This is a style issue and you will get many different answers from many different people. It is not a question of correctness. – Flexo Nov 01 '10 at 16:51
  • 1
    Many, many times duplicated. Searching now...http://stackoverflow.com/questions/377164/whats-your-preferred-pointer-declaration-style-and-why http://stackoverflow.com/questions/558474/what-makes-more-sense-char-string-or-char-string http://stackoverflow.com/questions/398395/in-c-why-is-the-asterisk-before-the-variable-name-rather-than-after-the-type etc... – dmckee --- ex-moderator kitten Nov 01 '10 at 16:51
  • 1
    I believe `string* str` is better, but I still voted to close since there is no "best" way. – Starkey Nov 01 '10 at 16:53
  • http://www2.research.att.com/~bs/bs_faq2.html#whitespace – aschepler Nov 01 '10 at 16:53
  • I disagree with you regarding the "subjective" argument, I wanted to know if there was any side effect to those notations. I got exactly the kind of answer I was looking for with the answer I accepted, see below. – Matthieu Napoli Nov 01 '10 at 17:08
  • I removed the subjectivity in my question to make it more clear. – Matthieu Napoli Nov 01 '10 at 17:10
  • @Matthieu: It doesn't matter that you don't mean to spark an argument or that you phrase the question very carefully. This is one of those touchy subjects that is an argument waiting to happen. Like emacs-vs-vi or early subprogram exits or when is it OK to use a goto. You can't bring it up without a crowd gathering to watch the fight. The same thing goes for the proper positions in of the braces in c like languages. But I voted for "exact duplicate". – dmckee --- ex-moderator kitten Nov 01 '10 at 20:53

4 Answers4

9

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;
fredoverflow
  • 256,549
  • 94
  • 388
  • 662
Benoit
  • 76,634
  • 23
  • 210
  • 236
3

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.

vitaut
  • 49,672
  • 25
  • 199
  • 336
3

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;
Lou Franco
  • 87,846
  • 14
  • 132
  • 192
2

In C++ - neither - this should be const string& str.

Or is that const string &str ? Hmm.

Steve Townsend
  • 53,498
  • 9
  • 91
  • 140