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

📄 ei27.htm

📁 一个非常适合初学者入门的有关c++的文档
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Frameset//EN" "http://www.w3.org/TR/REC-html40/frameset.dtd">
<HTML LANG="EN">
<HEAD>
<title>Effective C++, 2E | Item 27: Explicitly disallow use of implicitly generated member functions you don't want</TITLE>
<LINK REL=STYLESHEET HREF=../INTRO/ECMEC.CSS>

<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/COOKIE.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">var imagemax = 0; setCurrentMax(0);</SCRIPT>
<SCRIPT LANGUAGE="Javascript" SRC="../JAVA/DINGBATS.JS"></SCRIPT>
<SCRIPT LANGUAGE="Javascript">
var dingbase = "EI27_DIR.HTM";
var dingtext = "Item E27, P";
if (self == top) {
 top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>

</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E27: Disallow unwanted implicitly-generated funcs" -->
<A NAME="6406"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI26_FR.HTM" TARGET="_top">Item 26: Guard against potential ambiguity. </A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI28_FR.HTM" TARGET="_top">Item 28: Partition the global namespace.</A></FONT></DIV>

<P><A NAME="dingp1"></A><FONT ID="eititle">Item 27: &nbsp;Explicitly disallow use of implicitly generated member functions you don't want.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>

<A NAME="6407"></A>
<P><A NAME="dingp2"></A>
Suppose you want to write a class template, <CODE>Array</CODE>, whose generated classes behave like built-in C++ arrays in every way, except they perform bounds checking. One of the design problems you would face is how to prohibit assignment between <CODE>Array</CODE> objects, because assignment isn't legal for C++ <NOBR>arrays:<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="6408"></A>
<UL><PRE>double values1[10];
double values2[10];
</PRE>
</UL><A NAME="6409"></A>
<UL><PRE>values1 = values2;                 // error!
</PRE>
</UL><A NAME="6411"></A>
<P><A NAME="dingp3"></A>
For most functions, this wouldn't be a problem. If you didn't want to allow a function, you simply wouldn't put it in the class. However, the assignment operator is one of those distinguished member functions that C++, always the helpful servant, writes for you if you neglect to write it yourself (see <A HREF="./EI45_FR.HTM#8160" TARGET="_top">Item 45</A>). What then to <NOBR>do?<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="26444"></A>
<P><A NAME="dingp4"></A>
The solution is to declare the function, <CODE>operator=</CODE> in this case, <I>private</I>. By declaring a member function explicitly, you prevent compilers from generating their own version, and by making the function private, you keep people from calling <NOBR>it.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<A NAME="6417"></A>
<P><A NAME="dingp5"></A>
However, the scheme isn't foolproof; member and friend functions can still call your private function. <I>Unless</I>, that is, you are clever enough not to <I>define</I> the function. Then if you inadvertently call the function, you'll get an error at link-time (see <A HREF="./EI46_FR.HTM#195225" TARGET="_top">Item 46</A>).<SCRIPT>create_link(5);</SCRIPT>
</P>
<A NAME="6422"></A>
<P><A NAME="dingp6"></A>
For <CODE>Array</CODE>, your template definition would start out like <NOBR>this:<SCRIPT>create_link(6);</SCRIPT>
</NOBR></P>
<A NAME="221975"></A>
<UL><PRE>template&lt;class T&gt;
class Array {
private:
  // Don't define this function!
  Array&amp; operator=(const Array&amp; rhs);
</PRE>
</UL><A NAME="221976"></A>
<UL><PRE>  ...
</PRE>
</UL><A NAME="6426"></A>
<UL><PRE>};
</PRE>
</UL><A NAME="6427"></A>
<P><A NAME="dingp7"></A>
<A NAME="p117"></A>Now if a client tries to perform assignments on <CODE>Array</CODE> objects, compilers will thwart the attempt, and if you inadvertently try it in a member or a friend function, the linker will <NOBR>yelp.<SCRIPT>create_link(7);</SCRIPT>
</NOBR></P>
<A NAME="223182"></A>
<P><A NAME="dingp8"></A>
Don't assume from this example that this Item applies only to assignment operators. It doesn't. It applies to each of the compiler-generated functions described in <A HREF="./EI45_FR.HTM#8160" TARGET="_top">Item 45</A>. In practice, you'll find that the behavioral similarities between assignment and copy construction (see Items <A HREF="./EI11_FR.HTM#2042" TARGET="_top">11</A> and <A HREF="./EI16_FR.HTM#2225" TARGET="_top">16</A>) almost always mean that anytime you want to disallow use of one, you'll want to disallow use of the other, <NOBR>too.<SCRIPT>create_link(8);</SCRIPT>
</NOBR></P>

<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI26_FR.HTM" TARGET="_top">Item 26: Guard against potential ambiguity. </A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI28_FR.HTM" TARGET="_top">Item 28: Partition the global namespace.</A></FONT></DIV>

</BODY>
</HTML>

⌨️ 快捷键说明

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