< C++ > Default Arguments

摘自 C++ primer 5th edition:

Let’s look at another topic from C++’s bag of new tricks: the default argument. A default argu-
ment is a value that’s used automatically if you omit the corresponding actual argument from a
function call. For example, if you set up void wow(int n) so that n has a default value of 1,
the function call wow() is the same as wow(1). This gives you flexibility in how you use a func-
tion. Suppose you have a function called left() that returns the first n characters of a string,
with the string and n as arguments. More precisely, the function returns a pointer to a new
string consisting of the selected portion of the original string. For example, the call
left(“theory”, 3) constructs a new string “the” and returns a pointer to it. Now suppose
you establish a default value of 1 for the second argument. The call left(“theory”, 3) would
work as before, with your choice of 3 overriding the default. But the call left(“theory”),
instead of being an error, would assume a second argument of 1 and return a pointer to the
string “t”. This kind of default is helpful if your program often needs to extract a one-charac-
ter string but occasionally needs to extract longer strings.


How do you establish a default value? You must use the function prototype. Because the
compiler looks at the prototype to see how many arguments a function uses, the function

prototype also has to alert the program to the possibility of default arguments. The method is

to assign a value to the argument in the prototype. For example, here’s the prototype fitting
this description of left():
char * left(const char * str, int n = 1);


You want the function to return a new string, so its type is char*, or pointer-to-char. You
want to leave the original string unaltered, so you use the const qualifier for the first argu-
ment. You want n to have a default value of 1, so you assign that value to n. A default argu-
ment value is an initialization value. Thus, the preceding prototype initializes n to the value 1.
If you leave n alone, it has the value 1, but if you pass an argument, the new value overwrites
the 1.


When you use a function with an argument list, you must add defaults from right to left. That
is, you can’t provide a default value for a particular argument unless you also provide defaults
for all the arguments to its right:
int harpo(int n, int m = 4, int j = 5);
int chico(int n, int m = 6, int j);
int groucho(int k = 1, int m = 2, int n = 3);
// VALID
// INVALID
// VALID
For example, the harpo() prototype permits calls with one, two, or three arguments:
beeps = harpo(2);
beeps = harpo(1,8);
beeps = harpo (8,7,6);
// same as harpo(2,4,5)
// same as harpo(1,8,5)
// no default arguments used

The actual arguments are assigned to the corresponding formal arguments from left to right;
you can’t skip over arguments. Thus, the following isn’t allowed:
beeps = harpo(3, ,8);
// invalid, doesn’t set m to 4

Default arguments aren’t a major programming breakthrough; rather, they are a convenience.
When you begin working with class design, you’ll find that they can reduce the number of
constructors, methods, and method overloads you have to define.


Listing 8.9 puts default arguments to use. Note that only the prototype indicates the default.
The function definition is the same as it would be without default arguments.


LISTING 8.9
left.cpp
// left.cpp -- string function with a default argument
#include <iostream>
const int ArSize = 80;
char * left(const char * str, int n = 1);
int main()
{
using namespace std;
char sample[ArSize];
cout << “Enter a string:\n”;
cin.get(sample,ArSize);
char *ps = left(sample, 4);
cout << ps << endl;

delete [] ps;
// free old string
ps = left(sample);
cout << ps << endl;
delete [] ps;
// free new string
return 0;
}
// This function returns a pointer to a new string
// consisting of the first n characters in the str string.
char * left(const char * str, int n)
{
if(n < 0)
n = 0;
char * p = new char[n+1];
int i;
for (i = 0; i < n && str[i]; i++)
p[i] = str[i]; // copy characters
while (i <= n)
p[i++] = ‘\0’; // set rest of string to ‘\0’
return p;
}
Here’s a sample run of the program in Listing 8.9:
Enter a string:
forthcoming
fort
f


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值