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

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

    If I were to hazard a guess, I would say it's because C++ people are more likely to consider type information to be a thing in and of itself because templates make it possible to manipulate types programatically at compile time. They are also much less likely to declare several variables in the same declaration.

    The T* style focuses the type information in one place and makes it stand out, and the confusion that would be introduced by something like T* foo, bar; with this syntax is a non-issue if you never declare two variables in the same statement.

    Personally, I find the T* style to be really obnoxious and dislike it immensely. The * is part of the type information, it's true, but the way the compiler parses it makes it actually attached to the name, not the type. I think the T* way obscures something important that's happening.

    In my observations, it seems like I'm a rarity in the C++ community. I've noticed the same thing you have about what style is the most popular.

    To be clear, of course, either style works in either language. But I do notice the same thing you do, that one style tends to be a bit more common with C code, and the other a bit more common with C++.

提交回复
热议问题