为什么想到了'T *名称'到是C方式和'T *名称'是C ++的方式? [英] Why is it thought of 'T *name' to be the C way and 'T* name' to be the C++ way?
问题描述
请注意:这个问题是关于位置的星号( *
)
Note: This question is about the position of the asterisk (*
).
在大多数C code我看到(例如,在 Beej的指南网络编程),所有的变量声明/定义使用 T *名称
格式,即绑定 *
来的变量名。指针被认为属于该变量,而不是类型的
In most C code I see (e.g., in Beej's guide to network programming), all variable declarations / definitions use the T *name
format, i.e., bind the *
to the variable name. The pointer is thought of belonging to the variable, not the type.
在大多数C ++ code我看到的,格式为 T *名称
,也就是说,它结合了 *
来的变量的类型。指针被认为属于类型,而不是可变的。我自己,作为一个纯粹的C ++ codeR,也可以使用这种格式,为的指针到类型的显然(对我来说)所属的类型,而不是变量。 (顺便说一句,即便是C ++标准使用这种格式的例子。:))
In most C++ code I see, the format is T* name
, i.e., it binds the *
to the type of the variable . The pointer is thought of belonging to the type, not the variable. I myself, as a pure C++ coder, also use this format, as a pointer-to-type clearly (for me) belongs to the type, not the variable. (Incidently, even the C++ standard uses this format in the examples. :) )
是否有(历史的)原因?有当程序员开始做C ++思维只是改变了?
Is there a (historic) reason for this? Has the way of thinking just changed when programmers started doing C++?
这也将是很好,如果一个C codeR(使用前格式),可为什么他/她用它解释,而不是后者。
It would also be nice if a C coder (that uses the former format) could explain on why s/he uses it, and not the latter.
推荐答案
一个典型的C程序员写道: INT * P;
,并解释为 * P
的是什么 INT
,强调语法,以及可能指向C(和C ++)声明语法主张风格的正确性。事实上, *
绑定到语法名称 P
。
A "typical C programmer" writes
int *p;
and explains it "*p
is what is theint
" 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 namep
in the grammar.
一个典型的C ++程序员写道: INT * P;
,并解释为 P
是一个指向 INT
强调类型。事实上, P
的类型是为int *
。我清楚地preFER的重视,并看到它使用C ++更先进的零部件和重要。
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.
这篇关于为什么想到了'T *名称'到是C方式和'T *名称'是C ++的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!