All programming languages I am familiar to (C/C++, Java, C#, Objective C) accept both notations. So I want to 开发者_JAVA百科know which is semantically correct: Object* myObj
or Object *myObj
?
Well, it depends. Both are correct but I generally use the Object*, because the pointer itself can be considered a type and it is more readable.
But there is a problem with that. The pointer in C, by real, is just a modification of a type, and not a real type. If you declare multiple variables in one single line as this:
Object *a, b;
You will have a
as a pointer to Object and b
as one instance of Object
, so I imagine that the correct way is to put the pointer with the variable.
Which is semantically correct?
Semantically they are identical. Therefore which you use is a matter of taste.
Which taste should you prefer? Well, you can write object* a, b
and think that you have declared two pointers. Of course you have not, you have declared a
to be a pointer and b
as an object.
So you should certainly prefer object *a, b
to object* a, b
. However, I believe it is better still to have a single line for each variable declaration:
object *a;
object b;
This approach has no scope for confusion and is recommended by many coding style guidelines.
精彩评论