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

后端 未结 10 954
梦谈多话
梦谈多话 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: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.

提交回复
热议问题