我们再来一次:int * n或int * n [英] Here we go again: int *n or int* n
问题描述
我知道前者关于
后者的所有宗教论点,但有谁知道为什么Bjarne Stroustrup似乎更喜欢
int * n?愚蠢的问题,但我不得不问。
I''m aware of all the religious arguments of the former over the
latter, but does anyone know why Bjarne Stroustrup seems to prefer
int* n ? Dumb question, but I just had to ask.
推荐答案
5月29日下午5:20,Sal< h ... @ softcom。 netwrote:
On May 29, 5:20 pm, Sal <h...@softcom.netwrote:
我知道前者对
后者的所有宗教论点,但是有谁知道为什么Bjarne Stroustrup似乎更喜欢
int * n?愚蠢的问题,但我只是要问。
I''m aware of all the religious arguments of the former over the
latter, but does anyone know why Bjarne Stroustrup seems to prefer
int* n ? Dumb question, but I just had to ask.
我个人更喜欢
int * n来表示指向整数的指针。
int& n表示对整数的引用。
& n表示n的地址。
* n表示取消引用名为n的指针。
Personally I prefer
int * n to denote a pointer to an integer.
int & n to denote a reference to an integer.
&n to denote the address of n.
*n to denote dereferencing of a pointer named n.
Sal写道:
Sal wrote:
我知道前者对
$的所有宗教争论b $ b后者,但有谁知道为什么Bjarne Stroustrup似乎更喜欢
int * n?愚蠢的问题,但我只是要问。
I''m aware of all the religious arguments of the former over the
latter, but does anyone know why Bjarne Stroustrup seems to prefer
int* n ? Dumb question, but I just had to ask.
我不知道BS的推理,但我读了int * n。 asn是一个整数
指针。
" int * n"似乎说如果你取消引用n,你得到一个整数。
前者对我来说似乎更直观。
那个据说,Sutter和Alexandrescu的C ++编码标准第0项
说不要冒汗小东西。换句话说,不要太担心,这只是关于品味的问题。
Chris Gordon-Smith
www.simsoup.info
Chris Gordon-Smith写道:
Chris Gordon-Smith wrote:
我不知道BS的推理,但我读到了int * n。 asn是一个整数
指针。
" int * n"似乎说如果你取消引用n,你得到一个整数。
前者对我来说似乎更直观。
I don''t know BS''s reasoning, but I read "int* n" as "n is an integer
pointer".
"int *n" seems to say "If you dereference n, you get to an integer".
The former seems more intuitive to me.
它可能更直观(也就是我使用的那个,因为这个原因),
但是这样想是这样的一个令人尴尬的问题:
int * p1,p2;
有了这个想法,你也会认为你正在创造两个人/>
上面的指针,实际上你不是(你正在创建一个名为p1的
int指针和一个名为p2的int变量)。
纠正这一点仍然保持直觉并不容易。我个人通常做的只是避免问题并使用两行:
int * p1;
int * p2;
但是,它更详细。
It may be more intuitive (and is what I use as well, for that reason),
but thinking about it like that causes an embarrassing problem:
int* p1, p2;
With that thinking you''d also think that you are creating two
int-pointers above, when in fact you are not (you are creating an
int-pointer named p1 and an int variable named p2).
Correcting that while still keeping the intuition is not easy. What I
personally usually do is to just avoid the problem and use two lines:
int* p1;
int* p2;
It''s more verbose, though.
这篇关于我们再来一次:int * n或int * n的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!