Why is it thought of 'T *name' to be the C way and 'T* name' to be the C++ way?

后端 未结 10 877
梦谈多话
梦谈多话 2021-01-07 22:18

Note: This question is about the position of the asterisk (*).

In most C code I see (e.g., in Beej\'s guide to network programming), all variable declar

相关标签:
10条回答
  • 2021-01-07 22:41

    A couple of people mentioned very good points. I just want to note that while C has kept the meaning "declaration reflects use" very strongly, C++ didn't. Several declarators in C++ don't reflect the use in declarations;

    int &ref; // but "&ref" has type "int*", not "int".
    int &&ref; // but "&&ref" is not valid at all.
    

    I don't think that C++ programmers writing it different actually have that as their reason, but it may be something stopping C++ book writers from mentioning it, which may influence those C++ programmers.

    0 讨论(0)
  • 2021-01-07 22:42

    From Stroustrup's C++ Style and Technique FAQ.

    A "typical C programmer" writes int *p; and explains it "*p is what is the int" emphasizing syntax, and may point to the C (and C++) declaration grammar to argue for the correctness of the style. Indeed, the * binds to the name p in the grammar.

    A "typical C++ programmer" writes int* p; and explains it "p is a pointer to an int" emphasizing type. Indeed the type of p is int*. I clearly prefer that emphasis and see it as important for using the more advanced parts of C++ well.

    0 讨论(0)
  • 2021-01-07 22:44

    Personally, I would disagree with this -- I don't think there is a C or C++ way of doing this. I've never seen any evidence to suggest that. However, I do have a hypothesis as to why the syntax with the asterisk closer to the declared variable name in C.

    In C (at least, in C89, the vast majority of C in use), you must declare all your variables at the top of a block. This leads to programmers doing

    T a, b, c;
    

    relatively often, while in C++ this is rare. (In C++ you declare variables close to where they are used, and variables often have constructor parameters, etc; therefore it's rare to declare multiple objects on a single line)

    This also happens to be the only case where the syntax matters, because the statement

    T* a, b, c;
    

    declares one pointer, rather than the three the programmer might expect.

    Since putting more than one declaration on a single line is more common in C, and also is the only case where the position of the asterisk matters, it's more likely for the programmer to associate the asterisk with the variable.

    That said, I've never seen any evidence of this -- this is just conjecture.

    0 讨论(0)
  • 2021-01-07 22:48

    Just to throw some controversy into the matter:

    • C++ users commonly state that one declaration per line makes it okay to put the asterisk on the type (the left). int* a; int* b;
    • Both C and C++ are parsed with the asterisk attached to the name (on the right). int *a;
    • C++ preprocessors often move the asterisk to the left. C preprocessors move it to the right.
    • In either language, putting it on the left will look weird for declaring multiple variables of the same type in the same statement. int* a, * b, * c;
    • There are some syntactic elements that look stupid with it on the left. int* (* func)().

    There are other elements of C that are incorrectly used, such as const, which belongs on the right too! int const a;. This is commonly put on the left of the type, despite belonging to the type to its left. Many compilers have to be modified to handle this usage. In C++ you can observe this is conformed to with constant methods, as the compiler can't deduce for you if it's put on the left: int A::a() const. This also breaks down with nested usage of const, restricted and friends with such declarations as int const *. Conventional usage of const would suggest this is a constant pointer to an integer, but that's actually int *const.

    tl;dr

    You've all been doing it wrong all along, put everything on the right.

    0 讨论(0)
提交回复
热议问题