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

📄 edesignc.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 | Classes and Functions: Design and Declaration</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>
var dingbase = "EDESGDIR.HTM";
var dingtext = "EC++ Class/Func Design, P";
if (self == top) {
 top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>

</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<a name="classtop"></a>
<!-- SectionName="EC++ Chapter Intro: Design of Classes/Functions" -->

<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI17_FR.HTM" TARGET="_top">Item 17: Check for assignment to self in operator=. </A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI18_FR.HTM" TARGET="_top">Item 18: Strive for class interfaces that are complete and minimal.</A></FONT></DIV>

<P><A NAME="dingp1"></A><A NAME="p77"></A><FONT ID="egtitle">Classes and Functions: Design and Declaration</FONT><SCRIPT>create_link(1);</SCRIPT>

</P>

<A NAME="2327"></A>
<A NAME="2328"></A>
<P><A NAME="dingp2"></A>
Declaring a new class in a program creates a new type: class design is <I>type</I> design. You probably don't have much experience with type design, because most languages don't offer you the opportunity to get any practice. In C++, it is of fundamental importance, not just because you can do it if you want to, but because you <I>are</I> doing it every time you declare a class, whether you mean to or <NOBR>not.<SCRIPT>create_link(2);</SCRIPT>

</NOBR></P>
<A NAME="2329"></A>
<P><A NAME="dingp3"></A>
Designing good classes is challenging because designing good types is challenging. Good types have a natural syntax, an intuitive semantics, and one or more efficient implementations. In C++, a poorly thought out class definition can make it impossible to achieve any of these goals. Even the performance characteristics of a class's member functions are determined as much by the declarations of those member functions as they are by their <NOBR>definitions.<SCRIPT>create_link(3);</SCRIPT>

</NOBR></P>
<A NAME="2330"></A>
<P><A NAME="dingp4"></A>
How, then, do you go about designing effective classes? First, you must understand the issues you face. Virtually every class requires that you confront the following questions, the answers to which often lead to constraints on your design:<SCRIPT>create_link(4);</SCRIPT>

<UL><A NAME="2331"></A>
<A NAME="dingp5"></A><LI><I>How should objects be created and destroyed?</I> How this is done strongly influences the design of your constructors and destructor, as well as your versions of <CODE>operator</CODE> <CODE>new</CODE>, <CODE>operator</CODE> <CODE><NOBR>new[]</NOBR></CODE>, <CODE>operator</CODE> <CODE>delete</CODE>, and <CODE>operator</CODE> <nobr><CODE>delete[]</CODE></nobr>, if you write them. (<A HREF="../MEC/MI8_FR.HTM#33985" TARGET="_top">Item M8</A> describes the differences among these terms.)<SCRIPT>create_link(5);</SCRIPT>

<A NAME="2332"></A>
<A NAME="dingp6"></A><LI><I>How does object initialization differ from object assignment?</I> The answer to this question determines the behavior of and the differences between your constructors and your assignment operators.<SCRIPT>create_link(6);</SCRIPT>

<A NAME="2333"></A>
<A NAME="dingp7"></A><LI><I>What does it mean to pass objects of the new type by value?</I> Remember, the copy constructor defines what it means to pass an object by value.<SCRIPT>create_link(7);</SCRIPT>

<A NAME="5765"></A>
<A NAME="dingp8"></A><LI><I><A NAME="p78"></A>What are the constraints on legal values for the new type?</I> These constraints determine the kind of error checking you'll have to do inside your member functions, especially your constructors and assignment operators. It may also affect the exceptions your functions throw and, if you use them, your functions' exception specifications (see <A HREF="../MEC/MI14_FR.HTM#6011" TARGET="_top">Item M14</A>).<SCRIPT>create_link(8);</SCRIPT>

<A NAME="16146"></A>
<A NAME="dingp9"></A><LI><I>Does the new type fit into an inheritance graph?</I> If you inherit from existing classes, you are constrained by the design of those classes, particularly by whether the functions you inherit are virtual or nonvirtual. If you wish to allow other classes to inherit from your class, that will affect whether the functions you declare are virtual.<SCRIPT>create_link(9);</SCRIPT>

<A NAME="16148"></A>
<A NAME="dingp10"></A><LI><I>What kind of type conversions are allowed?</I> If you wish to allow objects of type A to be <I>implicitly</I> converted into objects of type B, you will want to write either a type conversion function in class A or a non-<CODE>explicit</CODE> constructor in class B that can be called with a single argument. If you wish to allow <I>explicit</I> conversions only, you'll want to write functions to perform the conversions, but you'll want to avoid making them type conversion operators or non-<CODE>explicit</CODE> single-argument constructors. (<A HREF="../MEC/MI5_FR.HTM#5970" TARGET="_top">Item M5</A> discusses the advantages and disadvantages of user-defined conversion functions.)<SCRIPT>create_link(10);</SCRIPT>

<A NAME="16149"></A>
<A NAME="dingp11"></A><LI><I>What operators and functions make sense for the new type?</I> The answer to this question determines which functions you'll declare in your class interface.<SCRIPT>create_link(11);</SCRIPT>

<A NAME="16150"></A>
<A NAME="dingp12"></A><LI><I>What standard operators and functions should be explicitly disallowed?</I> Those are the ones you'll need to declare <CODE>private</CODE>.<SCRIPT>create_link(12);</SCRIPT>

<A NAME="16151"></A>
<A NAME="dingp13"></A><LI><I>Who should have access to the members of the new type?</I> This question helps you determine which members are public, which are protected, and which are private. It also helps you determine which classes and/or functions should be friends, as well as whether it makes sense to nest one class inside another.<SCRIPT>create_link(13);</SCRIPT>

<A NAME="16165"></A>
<A NAME="dingp14"></A><LI>How general is the new type? Perhaps you're not really defining a new type. Perhaps you're defining a whole <I>family</I> of types. If so, you don't want to define a new class, you want to define a new class <I>template</I>.<SCRIPT>create_link(14);</SCRIPT>

</UL></P>
<A NAME="16154"></A>
<P><A NAME="dingp15"></A>
These are difficult questions to answer, so defining effective classes in C++ is far from simple. Done properly, however, user-defined classes in C++ yield types that are all but indistinguishable from built-in types, and that makes all the effort <NOBR>worthwhile.<SCRIPT>create_link(15);</SCRIPT>

</NOBR></P>
<A NAME="17772"></A>
<P><A NAME="dingp16"></A>
A discussion of the details of each of the above issues would comprise a book in its own right, so the guidelines that follow are anything but <A NAME="p79"></A>comprehensive. However, they highlight some of the most important design considerations, warn about some of the most frequent errors, and provide solutions to some of the most common problems encountered by class designers. Much of the advice is as applicable to non-member functions as it is to member functions, so in this section I consider the design and declaration of global and namespace-resident functions, <NOBR>too.<SCRIPT>create_link(16);</SCRIPT>

</NOBR></P>

<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI17_FR.HTM" TARGET="_top">Item 17: Check for assignment to self in operator=. </A> &nbsp;&nbsp;<BR>&nbsp;&nbsp;Continue to <A HREF="./EI18_FR.HTM" TARGET="_top">Item 18: Strive for class interfaces that are complete and minimal.</A></FONT></DIV>
</BODY>
</HTML>

⌨️ 快捷键说明

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