# Unity I like to do my asterik after my type(Please Ignore/Close)

This topic is 3713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Everytime I start to think I'm getting a handle on C++, I go and read a thread about "Hello World" that contains 30+ posts concerning the merits of '\n' vs. std::endl and I realize how far I've yet to go ^^. I like to do my asterik after my type, personally.
[source lang = "CPP"]
char *pointer = new char;


The correct answer in a team environment, however, is to alternate methods. That way, when someone who is anal about it asks what you're thinking putting your asterik in the wrong place you can say: "That wasn't me. I do it right like you." :) EDIT: Turns out, I'm a moron. This was supposed to be a reply to this not a new topic :/. I apologize and if someone could just close this while I pull a paper sack over my head and go sit in the corner that would be just awesome. Thanks. [Edited by - joshuanrobinson2002 on July 22, 2008 4:24:46 PM]

##### Share on other sites
Actually, you should do std::auto_ptr<char> pointer(new char); -- that way you don't leak memory should an exception get thrown and don't have to argue about little, shiny stars in your code. [grin]

##### Share on other sites
There is no right or wrong way to lay out your original snippet (although the auto_ptr is the better option because, as Oxyd said, you don't have to worry about exception-safety so much - auto_ptr will clean itself up when it goes out of scope, thus mostly avoiding memory leaks). All of the below are perfectly legal in C++:

T *foo; // Your wayT* bar; // My wayT * abc; // Perfectly legal syntax also// The following are also legalT &def;T& ghi;T & jkl; // Not sure about this one, haven't tested it

I always prefer to keep the specifier next to the type, personally. I'm not too sure why, I think it's because it makes it immediately more obvious to me whether it's a pointer, reference or a simple value variable on the stack.

You'll likely find that your employer has a set of in-house standards about how to do things like this, comments, variable naming conventions etc. Stick to those or die.

##### Share on other sites
The compiler doesn't really care which side you put the *, and most programmers can cope just fine if they prefer one style but encounter the other, so this isn't such a huge issue. I personally think that
char* pointer = new char;
looks clearer because you are defining a variable (pointer) as a certain type (char*). It is, however, misleading, because the compiler will associate the * with the variable. You can see a potential problem when defining multiple variables.
char* var1, var2;

In this case var1 is a pointer, and var2 looks like a pointer but is actually a char.

Because of this, I always use put the * to the right
char *pointer = new char;char *var1, *var2;

Don't think of it as defining a variable (pointer) such that (pointer) is of type (char*). Rather, think of it as defining (pointer) such that what it points to (*pointer) is of type (char). That mindset is more along the way a C++ parser actually works, and will save some confusion when dealing with defining multiple variables.

It also works to help understand the more complicated ways of writing constants. Consider this:
const int *a;int const *b;int * const c;

In the first case we define a variable (a) such that (*a) is of type (const int). So (a) is not a constant, but (*a) is constant.

In the second case we define a variable (b) such that (const *b) is of type (int). Again the variable (b) itself is not constant, but what it refers to (*b) is constant.

In the third case we define a variable (const a) such that (*a) is of type (int). Here (a) explicitly is a constant, but (*a) is not.

##### Share on other sites
Quote:
 Original post by joshuanrobinson2002EDIT:Turns out, I'm a moron. This was supposed to be a reply to this not a new topic :/. I apologize and if someone could just close this while I pull a paper sack over my head and go sit in the corner that would be just awesome. Thanks.

Don't beat yourself up Joshua. I figured you meant to respond to another thread but rather than act on a hunch, I supplied your thread with it's title in order to draw your attention to it. I was waiting for you to ask for this thread to be closed. [smile]

##### Share on other sites

This topic is 3713 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

This topic is now closed to further replies.

1. 1
2. 2
Rutin
20
3. 3
4. 4
frob
15
5. 5

• 9
• 9
• 13
• 9
• 33
• ### Forum Statistics

• Total Topics
632592
• Total Posts
3007284

×