Is there a space after the *, or after the &?
No, there is not a space after the *. Take a look at this example:
value = *pointer;
Note how there’s not a space after the asterisk. Also note how the type of “value” and the type of “*pointer” are the same.
Thus, when you declare these variables, you should do it like this:
Type value; Type *pointer; value = *pointer;
Why would you ever want to put the asterisk next to the type name? Why would that ever make sense? I just don’t get it!
Tags: c++, declarations, pointers, style, types
January 11th, 2008 at 11:16 pm
Wow. I had never thought to ask this profound question. Now I will never need to.
January 15th, 2008 at 2:02 pm
Lets take a walk down fantasy lane. Imagine C++ is not retarded and doesn’t overload arbitrary symbols willy-nilly for the sake of appearing l33t.
Lets say that in order to declare a pointer of type Type, you call it TypePtr. This is almost the same as following every class declaration with typedef. Thus we have:
class Foo {
// C++ nonsense;
};
typedef Foo* FooPtr;
Also lets presume that C++ does not overload * for dereference. I mean why would you have to? Lets assume that every Ptr (or TypePtr) has a member called deref.
Now your code looks something like:
Type value;
TypePtr value_ptr;
value = value_ptr.deref();
If you follow this logic and undo the typedef, you get:
Type value;
Type* value_ptr;
value = *value_ptr;
I think this is a matter of preference, and as with all preferences and opinions, people tend to feel strongly about them. Good post though - I didn’t know why anyone would ever ver want to put the asterisk next to the variable