开发者

Object* or Object *? [closed]

开发者 https://www.devze.com 2023-04-05 07:07 出处:网络
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references,or expertise, but this question will likely solicit debate, a
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消