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

📄 [10] constructors, c++ faq lite.htm

📁 c++faq。里面有很多关于c++的问题的解答。
💻 HTM
📖 第 1 页 / 共 4 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3c.org/TR/1999/REC-html401-19991224/loose.dtd">
<!-- saved from url=(0043)http://www.sunistudio.com/cppfaq/ctors.html -->
<HTML><HEAD><TITLE>[10] Constructors, C++ FAQ Lite</TITLE>
<META http-equiv=Content-Type content="text/html; charset=gb2312">
<META http-equiv=Content-Language content=zh-cn>
<META content=ctors.html name=FILENAME>
<META content="[10] Constructors, 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="[10] Constructors, C++ FAQ Lite.files/cpp-faq.css" type=text/css 
rel=stylesheet></HEAD>
<BODY>
<H1><A name=top></A>[10] 构造函数<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 [10]:</H3>
<UL>
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.1]">[10.1] 
  构造函数做什么?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.2]">[10.2] 
  <TT>List&nbsp;x;</TT> 和 <TT>List&nbsp;x();</TT>有区别吗?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.3]">[10.3] 
  如何才能够使一个构造函数直接地调用另一个构造函数?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.4]">[10.4] 
  <TT>Fred</TT> 类的默认构造函数总是 <TT>Fred::Fred()</TT>吗?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.5]">[10.5] 当我建立一个 
  <TT>Fred</TT> 对象数组时,哪个构造函数将被调用?</A> <IMG alt=UPDATED! 
  src="[10] Constructors, C++ FAQ Lite.files/updated.gif"> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.6]">[10.6] 
  构造函数应该用“初始化列表”还是“赋值”?</A> <IMG alt=UPDATED! 
  src="[10] Constructors, C++ FAQ Lite.files/updated.gif"> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.7]">[10.7] 
  可以在构造函数中使用 <TT>this</TT> 指针吗?</A><IMG alt=UPDATED! 
  src="[10] Constructors, C++ FAQ Lite.files/updated.gif"> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.8]">[10.8] 
  什么是“命名的构造函数用法(Named Constructor Idiom)”?</A> <IMG alt=UPDATED! 
  src="[10] Constructors, C++ FAQ Lite.files/updated.gif"> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.9]">[10.9] 
  为何不能在构造函数的初始化列表中初始化静态成员数据?</A> <IMG alt=UPDATED! 
  src="[10] Constructors, C++ FAQ Lite.files/updated.gif"> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.10]">[10.10] 
  为何有静态数据成员的类得到了链接错误?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.11]">[10.11] 
  什么是“<TT>static</TT> initialization order fiasco”?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.12]">[10.12] 
  如何防止“<TT>static</TT> initialization order fiasco”?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.13]">[10.13] 
  对于静态数据成员,如何防止“<TT>static</TT> initialization order fiasco”?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.14]">[10.14] 
  如何处理构造函数的失败?</A> 
  <LI><A href="http://www.sunistudio.com/cppfaq/ctors.html#[10.15]">[10.15] 
  什么是“命名参数用法(Named Parameter Idiom)”?</A> <IMG alt=NEW! 
  src="[10] Constructors, C++ FAQ Lite.files/new.gif"> </LI></UL>
<P>
<HR>

<P><A name=[10.1]></A>
<DIV class=FaqTitle>
<H3>[10.1] 构造函数做什么?</H3></DIV>
<P>构造函数从无到有建立对象。 
<P>构造函数就象“初始化函数”。它将一连串的随意的内存位变成活的对象。至少它要初始化对象内部所使用的域。它还可以分配资源(内存、文件、信号、套接字等) 
<P>"ctor" 是构造函数(constructor)典型的缩写。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#top">Top</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#bottom">Bottom</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/dtors.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[10.2]></A>
<DIV class=FaqTitle>
<H3>[10.2]&nbsp; <TT>List&nbsp;x;</TT> 和 <TT>List&nbsp;x();有区别吗</TT>?</H3></DIV>
<P>有非常大的区别!
<P>假设<TT>List</TT> 是某个类的名称。那么函数<TT>f()</TT> 中声明了一个局部的 <TT>List</TT>对象,名称为 
<TT>x</TT>: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;void&nbsp;f()<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp;List&nbsp;x;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;Local&nbsp;object&nbsp;named&nbsp;<TT>x</TT>&nbsp;(of&nbsp;class&nbsp;<TT>List</TT>)</EM><TT><BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;} 
</TT></DIV>
<P>但是函数 <TT>g()</TT> 中声明了一个名称为<TT>x()</TT>的函数,它返回一个 <TT>List</TT>:
<P> 
<DIV 
class=CodeBlock><TT>&nbsp;void&nbsp;g()<BR>&nbsp;{<BR>&nbsp;&nbsp;&nbsp;List&nbsp;x();&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;Function&nbsp;named&nbsp;<TT>x</TT>&nbsp;(that&nbsp;returns&nbsp;a&nbsp;<TT>List</TT>)</EM><TT><BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;} 
</TT></DIV>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#top">Top</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#bottom">Bottom</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/dtors.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[10.3]></A>
<DIV class=FaqTitle>
<H3>[10.3] 如何才能够使一个构造函数直接地调用另一个构造函数?</H3></DIV>
<P>不行。 
<P>注意:如果你调用了另一个构造函数,编译器将初始化一个临时局部对象;而不是初始化<TT>this</TT>对象。你可以通过一个默认参数或在一个私有成员函数 
<TT>init()</TT> 中共享它们的公共代码来使两个构造函数结合起来。 
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#top">Top</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#bottom">Bottom</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/dtors.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

<P><A name=[10.4]></A>
<DIV class=FaqTitle>
<H3>[10.4] <TT>Fred</TT> 类的默认构造函数总是<TT>Fred::Fred()</TT>吗?</H3></DIV>
<P>不。“默认构造函数”是能够被无参数调用的构造函数。因此,一个不带参数的构造函数当然是默认构造函数: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Fred&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;Fred();&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;默认构造函数: 
能够被无参数调用</EM><TT><BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;}; 
</TT></DIV>
<P>然而,如果参数被提供了默认值,那么带参数的默认构造函数也是可能的: 
<P>
<DIV 
class=CodeBlock><TT>&nbsp;class&nbsp;Fred&nbsp;{<BR>&nbsp;public:<BR>&nbsp;&nbsp;&nbsp;Fred(int&nbsp;i=3,&nbsp;int&nbsp;j=5);&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;默认构造函数: 
能够被无参数调用</EM><TT><BR>&nbsp;&nbsp;&nbsp;</TT><EM>//&nbsp;...</EM><TT><BR>&nbsp;}; 
</TT></DIV>
<P><SMALL>[&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#top">Top</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/ctors.html#bottom">Bottom</A> |&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/inline-functions.html">Previous&nbsp;section</A> 
|&nbsp;<A 
href="http://www.sunistudio.com/cppfaq/dtors.html">Next&nbsp;section</A> 
]</SMALL> 
<HR>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -