⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 [8] references, c++ faq lite.htm

📁 c++faq。里面有很多关于c++的问题的解答。
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<!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&nbsp;&copy; 
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>&nbsp;void&nbsp;swap(int&amp;&nbsp;i,&nbsp;int&amp;&nbsp;j)<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp;int&nbsp;tmp&nbsp;=&nbsp;i;<BR>&nbsp;&nbsp;&nbsp;i&nbsp;=&nbsp;j;<BR>&nbsp;&nbsp;&nbsp;j&nbsp;=&nbsp;tmp;<BR>&nbsp;}<BR>&nbsp;<BR>&nbsp;int&nbsp;main()<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp;int&nbsp;x,&nbsp;y;<BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;&nbsp;&nbsp;swap(x,y);<BR>&nbsp;} 
</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) 将 
&amp; 从调用者移到了被调用者处,(2)消除了<TT>*</TT>s。换句话说,C 程序员会将 <TT>i</TT> 看作为宏 
<TT>(*p)</TT>,而 p 就是指向 <TT>x</TT> 的指针(例如,编译器自动地将潜在的指针解除引用;<TT>i++</TT>被改变为 
<TT>(*p)++</TT>;<TT>i&nbsp;=&nbsp;7</TT> 被自动地转变成 <TT>*p&nbsp;=&nbsp;7</TT>)。 
<P>很重要:请不要将引用看作为指向一个对象的奇异指针,即使引用经常是用汇编语言下的地址来实现的。引用就是对象。不是指向对象的指针,也不是对象的拷贝,就是对象。&nbsp; 

<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html#top">Top</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[8.2]></A>
<DIV class=FaqTitle>
<H3>[8.2] 给引用赋值,意味着什么?</H3></DIV>
<P>改变引用的“指示物”(引用所指的对象)。 
<P>请记住: 引用就是它的指示物,所以当改变引用的值时,也会改变其指示物的值。以编译器作者的行话来说,引用是一个“左值”(它可以出现在赋值运算符左边)。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html#top">Top</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[8.3]></A>
<DIV class=FaqTitle>
<H3>[8.3] 返回一个引用,意味着什么?</H3></DIV>
<P>意味着该函数调用可以出现在赋值运算符的左边。 
<P>最初这种能力看起来有些古怪。例如,没有人会认为表达式 <TT>f()&nbsp;=&nbsp;7</TT> 有意义。然而,如果 a 是一个 Array 
类,大多数人会认为 <TT>a[i]&nbsp;=&nbsp;7</TT> 有意义,即使 <TT>a[i]</TT> 实际上是一个函数调用的伪装(它调用了 
如下的 Array 类的 <TT>Array::operator[](int)</TT>)。 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Array&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;int&nbsp;size()&nbsp;const;<BR>&nbsp;&nbsp;&nbsp;float&amp;&nbsp;operator[]&nbsp;(int&nbsp;index);<BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;};<BR>&nbsp;<BR>&nbsp;int&nbsp;main()<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp;Array&nbsp;a;<BR>&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&lt;&nbsp;a.size();&nbsp;++i)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a[i]&nbsp;=&nbsp;7;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;这行调用了&nbsp;<TT>Array::operator[](int)</TT></EM><TT><BR>&nbsp;} 
</TT></DIV>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html#top">Top</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/references.html#bottom">Bottom</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/classes-and-objects.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Next&nbsp;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 + -