Jump to content
  • Advertisement
Sign in to follow this  
joshuanrobinson2002

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.

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
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 this post


Link to post
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 way
T* bar; // My way
T * abc; // Perfectly legal syntax also

// The following are also legal
T &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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by joshuanrobinson2002
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.


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 this post


Link to post
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.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!