实例讲解在C++的函数中变量参数及默认参数的使用

2020-01-06 14:20:40于丽

上一个示例演示以下重要概念:
在访问任何变量参数前,必须建立一个列表标记作为类型 va_list 的变量。在前面的示例中,该标记称为 vl。
使用 va_arg 宏访问各个参数。必须告知 va_arg 宏要检索的参数的类型,以便它可以从堆栈中传输正确的字节数。如果为 va_arg 指定的大小的类型与通过调用程序提供的类型不同,则结果是不可预知的。
应将使用 va_arg 宏获取的结果显式强制转换为所需类型。
必须调用宏以终止可变参数处理。va_end


默认参数
在许多情况下,函数具有不常使用的参数,因为使用默认值便已足够。为了解决此问题,默认参数工具允许为函数仅指定在给定调用中有意义的参数。为了阐释此概念,请考虑函数重载中所示的示例。


// Prototype three print functions.
int print( char *s );         // Print a string.
int print( double dvalue );      // Print a double.
int print( double dvalue, int prec ); // Print a double with a
// given precision.

在许多应用程序中,可为 prec 提供合理的默认值,从而消除对两个函数的需求:


// Prototype two print functions.
int print( char *s );          // Print a string.
int print( double dvalue, int prec=2 ); // Print a double with a
// given precision.

略微更改了 print 函数的实现以反映类型 double 仅存在一个此类函数这一事实:


// default_arguments.cpp
// compile with: /EHsc /c

// Print a double in specified precision.
// Positive numbers for precision indicate how many digits
// precision after the decimal point to show. Negative
// numbers for precision indicate where to round the number
// to the left of the decimal point.

#include <iostream>
#include <math.h>
using namespace std;

int print( double dvalue, int prec ) {
  // Use table-lookup for rounding/truncation.
  static const double rgPow10[] = { 
   10E-7, 10E-6, 10E-5, 10E-4, 10E-3, 10E-2, 10E-1, 10E0,
     10E1, 10E2, 10E3, 10E4, 10E5, 10E6
  };
  const int iPowZero = 6;
  // If precision out of range, just print the number.
  if( prec >= -6 && prec <= 7 )
   // Scale, truncate, then rescale.
   dvalue = floor( dvalue / rgPow10[iPowZero - prec] ) *
   rgPow10[iPowZero - prec];
  cout << dvalue << endl;
  return cout.good();
}

若要调用新的 print 函数,请使用如下代码:


print( d );  // Precision of 2 supplied by default argument.
print( d, 0 ); // Override default argument to achieve other
// results.

使用默认参数时,请注意以下几点: