阅读宣言 [英] reading a declaration
问题描述
// C指针
int y = 0;
int * ptr =& y;
读作ptr是一个整数指针,指向(取
地址)整数y。
// C ++参考
int x = 0;
int& ref = x;
这读到什么?
TIA,Alan
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:Top-发布。
问:usenet和电子邮件中最烦人的事情是什么?
Alan写道:
// C指针
int y = 0;
int * ptr =& y;
读取asptr是一个整数指针,指向(取整数y的
地址)。
是的。
// C ++参考
int x = 0;
int& ref = x;
这读起来是什么?
" ref是对int引用的引用(是别名) for)x。
在消息< 10 ************* @ news.supernews.com>中,艾伦
< al ** @ surfbest.net>写道// C指针
int y = 0;
int * ptr =& y;
读作ptr是一个整数指向(取
地址)整数y的指针。
// C ++参考
int x = 0;
int& ref = x;
这读起来是什么?
" ref是对整数变量x绑定的整数的引用。
-
Richard Herring
" ; Rolf Magnus < RA ****** @ t-online.de>在消息新闻中写道:cp ************* @ news.t-online.com ...Alan写道:
// C指针
int y = 0;
int * ptr =& y;
读作ptr是一个指向的整数指针(需要)整数y"
地址。
是的。
// C ++参考
int x = 0;
int& ref = x;
这读起来是什么?
" ref是对int引用的引用(是x的别名) 。
我知道你在说什么但是指的是 (或者绑定作为理查德·赫林写的那个
)并没有真正告诉读者什么是关系是什么,imo。参考文献真的是编译器
未修饰的指针:
// C ++参考
int x = 0;
int& ref = x;
// C指针
int y = 0;
int * ptr =& y; < br $>
....
//参考
cout<< x = << x<< ",ref =" << ref<< ENDL; // x = 0,ref = 0
a ++;
cout<< x = << x<< ",ref =" << ref<< ENDL; // x = 1,ref = 1
//指针
cout<< y = << y<< ",* ptr =" << * ptr<< ENDL; // y = 0,* ptr = 0
(* b)++;
cout<< y = << y<< ",* ptr =" << * ptr<< ENDL; // y = 1,* ptr = 1
" a ++"更优选的是(* b)++。但是我在声明参考时质疑术语的选择。
''&''表示''的地址''(为了向后兼容C)但是在
中
" int& ref = x或者句法等价物int& ref = x ,如果在''地址''的背景下阅读它,那就没有任何意义。
imo," int ref =& x"更有意义。
我想知道为什么Bjarne不只是用指针解决问题
(悬挂指针,指针不指向什么,无效*
指针等)然后使用编译器来解开它们;-)
Alan
// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
TIA, Alan
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Alan wrote:
// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
Yes.
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
"ref is a reference to int that refers to (is an alias name for) x".
In message <10*************@news.supernews.com>, Alan
<al**@surfbest.net> writes// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
"ref is a reference to integer that is bound to integer variable x."
--
Richard Herring
"Rolf Magnus" <ra******@t-online.de> wrote in message news:cp*************@news.t-online.com...Alan wrote:// C Pointer
int y = 0;
int* ptr = &y;
Reads as "ptr is an integer pointer that points to (takes the
address of) integer y".
Yes.
// C++ Reference
int x = 0;
int& ref = x;
What does this read as?
"ref is a reference to int that refers to (is an alias name for) x".
I know what you''re saying but "refers to" (or "bound to" as
Richard Herring writes) doesn''t really tell the reader what
the relationship is, imo. References are really compiler
undecorated pointers:
// C++ Reference
int x = 0;
int& ref = x;
// C Pointer
int y = 0;
int* ptr = &y;
....
// Reference
cout << "x = " << x << ", ref = " << ref << endl; // x = 0, ref = 0
a++;
cout << "x = " << x << ", ref = " << ref << endl; // x = 1, ref = 1
// Pointer
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 0, *ptr = 0
(*b)++;
cout << "y = " << y << ", *ptr = " << *ptr << endl; // y = 1, *ptr = 1
"a++" is far more preferable to "(*b)++" but I question the choice of terminology in declaring a reference.
''&'' denotes ''address of'' (for backward compatibility with C) but in
"int& ref = x" or the syntactical equivalent "int &ref = x" , it doesn''t
make any sense if it is read in the context of ''address of''.
imo, "int ref = &x" makes more sense.
I wonder why Bjarne didn''t just fix the problems with pointers
(hanging pointers, pointers that don''t point to anything, void*
pointers, etc) and then use the compiler to undecorate them ;-)
Alan
这篇关于阅读宣言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!