📄 ei24.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 24: Choose carefully between function overloading and parameter defaulting</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 = "EI24_DIR.HTM";
var dingtext = "Item E24, P";
if (self == top) {
top.location.replace(dingbase + this.location.hash);
}
</SCRIPT>
</HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000" ONLOAD="setResize()">
<!-- SectionName="E24: Function overloading vs. parameter defaulting" -->
<A NAME="6250"></A>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI23_FR.HTM" TARGET="_top">Item 23: Don't try to return a reference when you must return an object. </A> <BR> Continue to <A HREF="./EI25_FR.HTM" TARGET="_top">Item 25: Avoid overloading on a pointer and a numerical type.</A></FONT></DIV>
<P><A NAME="dingp1"></A><FONT ID="eititle">Item 24: Choose carefully between function overloading and parameter defaulting.</FONT><SCRIPT>create_link(1);</SCRIPT>
</P>
<A NAME="6252"></A>
<P><A NAME="dingp2"></A>
The confusion over function overloading and parameter defaulting stems from the fact that they both allow a single function name to be called in more than one <NOBR>way:<SCRIPT>create_link(2);</SCRIPT>
</NOBR></P>
<A NAME="6253"></A>
<UL><PRE>
void f(); // f is overloaded
void f(int x);
</PRE>
</UL><A NAME="6254"></A>
<UL><PRE>
f(); // calls f()
f(10); // calls f(int)
</PRE>
</UL><A NAME="6255"></A>
<UL><PRE>
void g(int x = 0); // g has a default
// parameter value
</PRE>
</UL><A NAME="6256"></A>
<UL><PRE>
g(); // calls g(0)
g(10); // calls g(10)
</PRE>
</UL><A NAME="6257"></A>
<P><A NAME="dingp3"></A>
So which should be used <NOBR>when?<SCRIPT>create_link(3);</SCRIPT>
</NOBR></P>
<A NAME="6258"></A>
<P><A NAME="dingp4"></A>
The answer depends on two other questions. First, is there a value you can use for a default? Second, how many algorithms do you want to use? In general, if you can choose a reasonable default value and you want to employ only a single algorithm, you'll use default parameters (see also <A HREF="./EI38_FR.HTM#177948" TARGET="_top">Item 38</A>). Otherwise you'll use function <NOBR>overloading.<SCRIPT>create_link(4);</SCRIPT>
</NOBR></P>
<A NAME="6262"></A>
<P><A NAME="dingp5"></A>
Here's a function to compute the maximum of up to five <CODE>int</CODE>s. This function uses — take a deep breath and steel yourself — <CODE>std::numeric_limits<int>::min()</CODE> as a default parameter value. I'll have more to say about that in a moment, but first, here's the <NOBR>code:<SCRIPT>create_link(5);</SCRIPT>
</NOBR></P>
<A NAME="6265"></A>
<UL><PRE>
int max(int a,
int b = std::numeric_limits<int>::min(),
int c = std::numeric_limits<int>::min(),
int d = std::numeric_limits<int>::min(),
int e = std::numeric_limits<int>::min())
{
int temp = a > b ? a : b;
temp = temp > c ? temp : c;
temp = temp > d ? temp : d;
return temp > e ? temp : e;
}
</PRE>
</UL><A NAME="31754"></A>
<P><A NAME="dingp6"></A>
<A NAME="p107"></A>Now, calm yourself. <CODE>std::numeric_limits<int>::min()</CODE> is just the fancy new-fangled way the standard C++ library says what C says via the <CODE>INT_MIN</CODE> macro in <CODE><limits.h></CODE>: it's the minimum possible value for an <CODE>int</CODE> in whatever compiler happens to be processing your C++ source code. True, it's a deviation from the terseness for which C is renowned, but there's a method behind all those colons and other syntactic <NOBR>strychnine.<SCRIPT>create_link(6);</SCRIPT>
</NOBR></P>
<A NAME="28783"></A>
<P><A NAME="dingp7"></A>
Suppose you'd like to write a function template taking any built-in numeric type as its parameter, and you'd like the functions generated from the template to print the minimum value representable by their instantiation type. Your template would look something like <NOBR>this:<SCRIPT>create_link(7);</SCRIPT>
</NOBR></P>
<A NAME="28786"></A>
<UL><PRE>template<class T>
void printMinimumValue()
{
cout << <I>the minimum value representable by T;</I>
}
</PRE>
</UL><A NAME="28790"></A>
<P><A NAME="dingp8"></A>
This is a difficult function to write if all you have to work with is <CODE><limits.h></CODE> and <CODE><float.h></CODE>. You don't know what <CODE>T</CODE> is, so you don't know whether to print out <CODE>INT_MIN</CODE> or <CODE>DBL_MIN</CODE> or <NOBR>what.<SCRIPT>create_link(8);</SCRIPT>
</NOBR></P>
<A NAME="28806"></A>
<P><A NAME="dingp9"></A>
To sidestep these difficulties, the standard C++ library (see <A HREF="./EI49_FR.HTM#8392" TARGET="_top">Item 49</A>) defines in the header <CODE><limits></CODE> a class template, <CODE>numeric_limits</CODE>, which itself defines several static member functions. Each function returns information about the type instantiating the template. That is, the functions in <CODE>numeric_limits<int></CODE> return information about type <CODE>int</CODE>, the functions in <CODE>numeric_limits<double></CODE> return information about type <CODE>double</CODE>, etc. Among the functions in <CODE>numeric_limits</CODE> is <CODE>min</CODE>. <CODE>min</CODE> returns the minimum representable value for the instantiating type, so <CODE>numeric_limits<int>::min()</CODE> returns the minimum representable integer <NOBR>value.<SCRIPT>create_link(9);</SCRIPT>
</NOBR></P>
<A NAME="28815"></A>
<P><A NAME="dingp10"></A>
Given <CODE>numeric_limits</CODE> (which, like nearly everything in the standard library, is in namespace <CODE>std</CODE> — see <A HREF="./EI28_FR.HTM#6429" TARGET="_top">Item 28</A>; <CODE>numeric_limits</CODE> itself is in the header <CODE><limits></CODE>), writing <CODE>printMinimumValue</CODE> is as easy as can <NOBR>be:<SCRIPT>create_link(10);</SCRIPT>
</NOBR></P>
<A NAME="28811"></A>
<UL><PRE>template<class T>
void printMinimumValue()
{
cout << std::numeric_limits<T>::min();
}
</PRE>
</UL><A NAME="28841"></A>
<P><A NAME="dingp11"></A>
This <CODE>numeric_limits</CODE>-based approach to specifying type-dependent constants may look expensive, but it's not. That's because the long-windedness of the source code fails to be reflected in the resultant <A NAME="p108"></A>object code. In fact, calls to functions in <CODE>numeric_limits</CODE> generate no instructions at all. To see how that can be, consider the following, which is an obvious way to implement <CODE>numeric_limits<int>::min</CODE>:<SCRIPT>create_link(11);</SCRIPT>
</P>
<A NAME="28845"></A>
<UL><PRE>#include <limits.h>
</PRE>
</UL><A NAME="28846"></A>
<UL><PRE>namespace std {
</PRE>
</UL><A NAME="28847"></A>
<UL><PRE> inline int numeric_limits<int>::min() throw ()
{ return INT_MIN; }
</PRE>
</UL><A NAME="28848"></A>
<UL><PRE>}
</PRE>
</UL><A NAME="28856"></A>
<P><A NAME="dingp12"></A>
Because this function is declared inline, calls to it should be replaced by its body (see <A HREF="./EI33_FR.HTM#6729" TARGET="_top">Item 33</A>). That's just <CODE>INT_MIN</CODE>, which is itself a simple <CODE>#define</CODE> for some implementation-defined constant. So even though the <CODE>max</CODE> function at the beginning of this Item looks like it's making a function call for each default parameter value, it's just using a clever way of referring to a type-dependent constant, in this case the value of <CODE>INT_MIN</CODE>. Such efficient cleverness abounds in C++'s standard library. You really should read <A HREF="./EI49_FR.HTM#8392" TARGET="_top">Item 49</A>.<SCRIPT>create_link(12);</SCRIPT>
</P>
<A NAME="17369"></A>
<P><A NAME="dingp13"></A>
Getting back to the <CODE>max</CODE> function, the crucial observation is that <CODE>max</CODE> uses the same (rather inefficient) algorithm to compute its result, regardless of the number of arguments provided by the caller. Nowhere in the function do you attempt to figure out which parameters are "real" and which are defaults. Instead, you have chosen a default value that cannot possibly affect the validity of the computation for the algorithm you're using. That's what makes the use of default parameter values a viable <NOBR>solution.<SCRIPT>create_link(13);</SCRIPT>
</NOBR></P>
<A NAME="6267"></A>
<P><A NAME="dingp14"></A>
For many functions, there is no reasonable default value. For example, suppose you want to write a function to compute the average of up to five <CODE>int</CODE>s. You can't use default parameter values here, because the result of the function is dependent on the number of parameters passed in: if 3 values are passed in, you'll divide their sum by 3; if 5 values are passed in, you'll divide their sum by 5. Furthermore, there is no "magic number" you can use as a default to indicate that a parameter wasn't actually provided by the client, because all possible <CODE>int</CODE>s are valid values for the parameters. In this case, you have no choice: you <I>must</I> use overloaded <NOBR>functions:<SCRIPT>create_link(14);</SCRIPT>
</NOBR></P>
<A NAME="6269"></A>
<UL><PRE>double avg(int a);
double avg(int a, int b);
double avg(int a, int b, int c);
double avg(int a, int b, int c, int d);
double avg(int a, int b, int c, int d, int e);
</PRE>
</UL><A NAME="6270"></A>
<P><A NAME="dingp15"></A>
<A NAME="p109"></A>The other case in which you need to use overloaded functions occurs when you want to accomplish a particular task, but the algorithm that you use depends on the inputs that are given. This is commonly the case with constructors: a default constructor will construct an object from scratch, whereas a copy constructor will construct one from an existing <NOBR>object:<SCRIPT>create_link(15);</SCRIPT>
</NOBR></P>
<A NAME="6273"></A>
<UL><PRE>// A class for representing natural numbers
class Natural {
public:
Natural(int initValue);
Natural(const Natural& rhs);
</PRE>
</UL><A NAME="17435"></A>
<UL><PRE>private:
unsigned int value;
</PRE>
</UL><A NAME="17436"></A>
<UL><PRE> void init(int initValue);
void error(const string& msg);
};
</PRE>
</UL><A NAME="6277"></A>
<UL><PRE>inline
void Natural::init(int initValue) { value = initValue; }
</PRE>
</UL><A NAME="6280"></A>
<UL><PRE>Natural::Natural(int initValue)
{
if (initValue > 0) init(initValue);
else error("Illegal initial value");
}
</PRE>
</UL><A NAME="6281"></A>
<UL><PRE>inline Natural::Natural(const Natural& x)
{ init(x.value); }
</PRE>
</UL><A NAME="6282"></A>
<P><A NAME="dingp16"></A>
The constructor taking an <CODE>int</CODE> has to perform error checking, but the copy constructor doesn't, so two different functions are needed. That means overloading. However, note that both functions must assign an initial value for the new object. This could lead to code duplication in the two constructors, so you maneuver around that problem by writing a private member function <CODE>init</CODE> that contains the code common to the two constructors. This tactic — using overloaded functions that call a common underlying function for some of their work — is worth remembering, because it's frequently useful (see e.g., <A HREF="./EI12_FR.HTM#2071" TARGET="_top">Item 12</A>).<SCRIPT>create_link(16);</SCRIPT>
</P>
<DIV ALIGN="CENTER"><FONT SIZE="-1">Back to <A HREF="./EI23_FR.HTM" TARGET="_top">Item 23: Don't try to return a reference when you must return an object. </A> <BR> Continue to <A HREF="./EI25_FR.HTM" TARGET="_top">Item 25: Avoid overloading on a pointer and a numerical type.</A></FONT></DIV>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -