📄 [8] references, c++ faq lite.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0048)http://www.sunistudio.com/cppfaq/references.html -->
<HTML><HEAD><TITLE>[8] References, C++ FAQ Lite</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-cn>
<META content=references.html name=FILENAME>
<META content="[8] References, C++ FAQ Lite" name=ABSTRACT>
<META content=cline@parashift.com name=OWNER>
<META content="Marshall Cline, cline@parashift.com" name=AUTHOR>
<META content="MSHTML 6.00.2462.0" name=GENERATOR>
<META content=FrontPage.Editor.Document name=ProgId><LINK rev=made
href="mailto:cline@parashift.com"><LINK
href="[8] References, C++ FAQ Lite.files/cpp-faq.css" type=text/css
rel=stylesheet></HEAD>
<BODY>
<H1><A name=top></A>[8] 引用<BR><SMALL><SMALL>(Part of <A
href="http://www.sunistudio.com/cppfaq/index.html"><EM>C++ FAQ Lite</EM></A>, <A
href="http://www.sunistudio.com/cppfaq/copy-permissions.html#[1.2]">Copyright ©
1991-2001</A>, <A href="http://www.parashift.com/" target=OutsideTheFAQ>Marshall
Cline</A>, <A
href="mailto:cline@parashift.com">cline@parashift.com</A>)</SMALL></SMALL></H1>
<P>简体中文版翻译:<A href="http://www.sunistudio.com/nicrosoft">申旻</A>,<A
href="mailto:nicrosoft@sunistudio.com">nicrosoft@sunistudio.com</A>(<A
href="http://www.sunistudio.com/">东日制作室</A>,<A
href="http://www.sunistudio.com/asp/sunidoc.asp">东日文档</A>)</P>
<HR>
<H3>FAQs in section [8]:</H3>
<UL>
<LI><A href="http://www.sunistudio.com/cppfaq/references.html#[8.1]">[8.1]
什么是引用?</A>
<LI><A href="http://www.sunistudio.com/cppfaq/references.html#[8.2]">[8.2]
给引用赋值意味着什么?</A>
<LI><A href="http://www.sunistudio.com/cppfaq/references.html#[8.3]">[8.3]
返回一个引用意味着什么?</A>
<LI><A href="http://www.sunistudio.com/cppfaq/references.html#[8.4]">[8.4]
<TT>object.method1().method2()</TT> 是什么意思?</A> <IMG alt=NEW!
src="[8] References, C++ FAQ Lite.files/new.gif">
<LI><A href="http://www.sunistudio.com/cppfaq/references.html#[8.5]">[8.5]
如何才能使一个引用指向另一个对象?</A>
<LI><A href="http://www.sunistudio.com/cppfaq/references.html#[8.6]">[8.6]
何时该使用引用,何时该使用指针?</A>
<LI><A href="http://www.sunistudio.com/cppfaq/references.html#[8.7]">[8.7]
什么是对象的句柄?它是指针吗?它是引用吗?它是指向指针的指针?它是什么?</A> <IMG alt=NEW!
src="[8] References, C++ FAQ Lite.files/new.gif"> </LI></UL>
<P>
<HR>
<P><A name=[8.1]></A>
<DIV class=FaqTitle>
<H3>[8.1] 什么是引用?</H3></DIV>
<P>对象的别名(改变的名称)。
<P>引用经常用于“按引用传递(pass-by-reference)”:
<P>
<DIV
class=CodeBlock><TT> void swap(int& i, int& j)<BR> {<BR> int tmp = i;<BR> i = j;<BR> j = tmp;<BR> }<BR> <BR> int main()<BR> {<BR> int x, y;<BR> </TT><EM>// ...</EM><TT><BR> swap(x,y);<BR> }
</TT></DIV>
<P>此处的 <TT>i</TT> 和 <TT>j</TT> 分别是main中的 <TT>x</TT> 和 <TT>y</TT>。换句话说,<TT>i</TT>
就是 <TT>x</TT> —— 并非指向 x 的指针,也不是 <TT>x </TT>的拷贝,而是 <TT>x</TT> 本身。对 <TT>i</TT>
的任何改变同样会影响 <TT>x</TT>,反之亦然。
<P>好,这就是作为一个程序员所认知的引用。现在,给你一个不同的视角,这可能会让你更糊涂,这就是引用是如何实现的。典型的情况下,对象 <TT>x</TT>
的引用 <TT>i</TT> 是 <TT>x</TT> 的机器地址。但是,当程序员写 <TT>i++</TT> 时,编译器产生增加 <TT>x</TT>
的代码。更详细的来说, 编译器用来寻找 <TT>x</TT> 的地址位并没有被改变。C 程序员将此认为好像是 C 风格的按指针传递,只是句法不同 (1) 将
& 从调用者移到了被调用者处,(2)消除了<TT>*</TT>s。换句话说,C 程序员会将 <TT>i</TT> 看作为宏
<TT>(*p)</TT>,而 p 就是指向 <TT>x</TT> 的指针(例如,编译器自动地将潜在的指针解除引用;<TT>i++</TT>被改变为
<TT>(*p)++</TT>;<TT>i = 7</TT> 被自动地转变成 <TT>*p = 7</TT>)。
<P>很重要:请不要将引用看作为指向一个对象的奇异指针,即使引用经常是用汇编语言下的地址来实现的。引用就是对象。不是指向对象的指针,也不是对象的拷贝,就是对象。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/references.html#top">Top</A> | <A
href="http://www.sunistudio.com/cppfaq/references.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[8.2]></A>
<DIV class=FaqTitle>
<H3>[8.2] 给引用赋值,意味着什么?</H3></DIV>
<P>改变引用的“指示物”(引用所指的对象)。
<P>请记住: 引用就是它的指示物,所以当改变引用的值时,也会改变其指示物的值。以编译器作者的行话来说,引用是一个“左值”(它可以出现在赋值运算符左边)。
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/references.html#top">Top</A> | <A
href="http://www.sunistudio.com/cppfaq/references.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[8.3]></A>
<DIV class=FaqTitle>
<H3>[8.3] 返回一个引用,意味着什么?</H3></DIV>
<P>意味着该函数调用可以出现在赋值运算符的左边。
<P>最初这种能力看起来有些古怪。例如,没有人会认为表达式 <TT>f() = 7</TT> 有意义。然而,如果 a 是一个 Array
类,大多数人会认为 <TT>a[i] = 7</TT> 有意义,即使 <TT>a[i]</TT> 实际上是一个函数调用的伪装(它调用了
如下的 Array 类的 <TT>Array::operator[](int)</TT>)。
<P>
<DIV
class=CodeBlock><TT> class Array {<BR> public:<BR> int size() const;<BR> float& operator[] (int index);<BR> </TT><EM>// ...</EM><TT><BR> };<BR> <BR> int main()<BR> {<BR> Array a;<BR> for (int i = 0; i < a.size(); ++i)<BR> a[i] = 7; </TT><EM>// 这行调用了 <TT>Array::operator[](int)</TT></EM><TT><BR> }
</TT></DIV>
<P><SMALL>[ <A
href="http://www.sunistudio.com/cppfaq/references.html#top">Top</A> | <A
href="http://www.sunistudio.com/cppfaq/references.html#bottom">Bottom</A>
| <A
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html">Previous section</A>
| <A
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Next section</A>
]</SMALL>
<HR>
<P><A name=[8.4]></A>
<DIV class=FaqTitle>
<H3>[8.4] <TT>object.method1().method2()</TT> 是什么意思? <IMG alt=NEW!
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -