作者zlw (洞房不败)
看板C_and_CPP
标题[问题] C++ FQA 文章中,有关 dangling reference 的内容
时间Mon Feb 23 19:44:02 2009
大家好
在 google 连到这一系列的文章,叫 C++ FQA Lite,网址:
http://yosefk.com/c++fqa/
大概看了一下,似乎是对 C++ FAQ Lite 的内容有些不同的看法,所写出来的文章
其中这篇讲到 C++ References - [8.1] What is a reference?
http://yosefk.com/c++fqa/ref.html#fqa-8.1
这段主要在反驳「reference 只是某物件的别名 (alias)」这件事
其中讲到 dangling references 做例子,我不太能理解,有没有前辈能举例解释一二?
节录部份如下:
------------------------------------------------------------------------------
For example, a wide class of bugs comes from accessing
dangling references - references to objects which were already destroyed.
If a reference is the object, or just another name for it, how can that happen?
------------------------------------------------------------------------------
我知道 dangling pointer 的意思以及怎样才会发生。
但上面说的这个 dangling reference 导致的 bug 要怎样才能重现我无法理解。
因为比如
main{
{
int nobj = 1;
int &nref = nobj;
}
/*
到这里 nobj 已经消灭,可是 nref 也消灭了。reference 又不能像指标
一样,晚点再决定参考谁,比如VC编译器就会反应"没有初始化" (error c2530)
那怎麽 dangling 呢?
*/
}
谢谢大家。
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 124.8.147.85
※ 编辑: zlw 来自: 124.8.147.85 (02/23 19:45)
1F:推 Fenikso:int& foo() {int x; return x;} 02/23 21:04