實例講解在C++的函數(shù)中變量參數(shù)及默認參數(shù)的使用
包含變量參數(shù)列表的函數(shù)
如果函數(shù)聲明中最后一個成員是省略號 (...),則函數(shù)聲明可采用數(shù)量可變的參數(shù)。在這些情況下,C++ 只為顯式聲明的參數(shù)提供類型檢查。即使參數(shù)的數(shù)量和類型是可變的,在需要使函數(shù)泛化時也可使用變量參數(shù)列表。函數(shù)的系列是一個使用變量參數(shù)列表的函數(shù)的示例。printfargument-declaration-list
包含變量參數(shù)的函數(shù)
若要訪問聲明后的參數(shù),請使用包含在標準包含文件 STDARG.H 中的宏(如下所述)。
采用數(shù)量可變的參數(shù)的函數(shù)聲明至少需要一個占位符參數(shù)(即使不使用它)。如果未提供此占位符參數(shù),則無法訪問其余參數(shù)。
當 char 類型的參數(shù)作為變量參數(shù)進行傳遞時,它們將被轉換為 int 類型。同樣,當 float 類型的參數(shù)作為變量參數(shù)進行傳遞時,它們將被轉換為 double 類型。其他類型的參數(shù)受常見整型和浮點型提升的限制。
使用參數(shù)列表中的省略號 (...) 來聲明需要變量列表的函數(shù)。使用在 STDARG.H 包含文件中描述的類型與宏來訪問變量列表所傳遞的參數(shù)。有關這些宏的詳細信息,請參閱 va_arg、va_copy、va_end、va_start。(處于 C 運行時庫文檔中)。
以下示例演示如何將宏與類型一起使用(在 STDARG.H 中聲明):va_listva_endva_argva_start
// variable_argument_lists.cpp
#include <stdio.h>
#include <stdarg.h>
// Declaration, but not definition, of ShowVar.
void ShowVar( char *szTypes, ... );
int main() {
ShowVar( "fcsi", 32.4f, 'a', "Test string", 4 );
}
// ShowVar takes a format string of the form
// "ifcs", where each character specifies the
// type of the argument in that position.
//
// i = int
// f = float
// c = char
// s = string (char *)
//
// Following the format specification is a variable
// list of arguments. Each argument corresponds to
// a format character in the format string to which
// the szTypes parameter points
void ShowVar( char *szTypes, ... ) {
va_list vl;
int i;
// szTypes is the last argument specified; you must access
// all others using the variable-argument macros.
va_start( vl, szTypes );
// Step through the list.
for( i = 0; szTypes[i] != '\0'; ++i ) {
union Printable_t {
int i;
float f;
char c;
char *s;
} Printable;
switch( szTypes[i] ) { // Type to expect.
case 'i':
Printable.i = va_arg( vl, int );
printf_s( "%i\n", Printable.i );
break;
case 'f':
Printable.f = va_arg( vl, double );
printf_s( "%f\n", Printable.f );
break;
case 'c':
Printable.c = va_arg( vl, char );
printf_s( "%c\n", Printable.c );
break;
case 's':
Printable.s = va_arg( vl, char * );
printf_s( "%s\n", Printable.s );
break;
default:
break;
}
}
va_end( vl );
}
//Output:
// 32.400002
// a
// Test string
上一個示例演示以下重要概念:
在訪問任何變量參數(shù)前,必須建立一個列表標記作為類型 va_list 的變量。在前面的示例中,該標記稱為 vl。
使用 va_arg 宏訪問各個參數(shù)。必須告知 va_arg 宏要檢索的參數(shù)的類型,以便它可以從堆棧中傳輸正確的字節(jié)數(shù)。如果為 va_arg 指定的大小的類型與通過調用程序提供的類型不同,則結果是不可預知的。
應將使用 va_arg 宏獲取的結果顯式強制轉換為所需類型。
必須調用宏以終止可變參數(shù)處理。va_end
默認參數(shù)
在許多情況下,函數(shù)具有不常使用的參數(shù),因為使用默認值便已足夠。為了解決此問題,默認參數(shù)工具允許為函數(shù)僅指定在給定調用中有意義的參數(shù)。為了闡釋此概念,請考慮函數(shù)重載中所示的示例。
// 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 提供合理的默認值,從而消除對兩個函數(shù)的需求:
// 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 函數(shù)的實現(xiàn)以反映類型 double 僅存在一個此類函數(shù)這一事實:
// 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 函數(shù),請使用如下代碼:
print( d ); // Precision of 2 supplied by default argument. print( d, 0 ); // Override default argument to achieve other // results.
使用默認參數(shù)時,請注意以下幾點:
默認參數(shù)僅在其中省略了尾隨參數(shù)的函數(shù)調用中使用 - 它們必須是最后的參數(shù)。因此,以下代碼是非法的:
int print( double dvalue = 0.0, int prec );
默認參數(shù)不能在以后的聲明中重新定義,即使重新定義的參數(shù)與原始參數(shù)相同也是如此。因此,以下代碼將生成錯誤:
// Prototype for print function.
int print( double dvalue, int prec = 2 );
...
// Definition for print function.
int print( double dvalue, int prec = 2 )
{
...
}
此代碼的問題在于定義中的函數(shù)聲明重新定義了 prec 的默認參數(shù)。
以后的聲明可添加額外的默認參數(shù)。
可為指向函數(shù)的指針提供默認參數(shù)。例如:
int (*pShowIntVal)( int i = 0 );
欄 目:C語言
本文標題:實例講解在C++的函數(shù)中變量參數(shù)及默認參數(shù)的使用
本文地址:http://www.jygsgssxh.com/a1/Cyuyan/2545.html
您可能感興趣的文章
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02C語言中怎么打出三角函數(shù) c語言中怎么打出三角函數(shù)的值
- 01-10APUE筆記之:進程環(huán)境詳解
- 01-10c++中inline的用法分析
- 01-10深入理解堆排序及其分析
- 01-10深入C/C++浮點數(shù)在內存中的存儲方式詳解
- 01-10貪心算法 WOODEN STICKS 實例代碼
- 01-10深入解析Linux下\r\n的問題
- 01-10基于getline()函數(shù)的深入理解
- 01-10深入理解C/C++混合編程


閱讀排行
本欄相關
- 04-02c語言函數(shù)調用后清空內存 c語言調用
- 04-02func函數(shù)+在C語言 func函數(shù)在c語言中
- 04-02c語言的正則匹配函數(shù) c語言正則表達
- 04-02c語言用函數(shù)寫分段 用c語言表示分段
- 04-02c語言中對數(shù)函數(shù)的表達式 c語言中對
- 04-02c語言編寫函數(shù)冒泡排序 c語言冒泡排
- 04-02c語言沒有round函數(shù) round c語言
- 04-02c語言分段函數(shù)怎么求 用c語言求分段
- 04-02C語言中怎么打出三角函數(shù) c語言中怎
- 04-02c語言調用函數(shù)求fibo C語言調用函數(shù)求
隨機閱讀
- 01-10delphi制作wav文件的方法
- 01-10SublimeText編譯C開發(fā)環(huán)境設置
- 08-05dedecms(織夢)副欄目數(shù)量限制代碼修改
- 08-05織夢dedecms什么時候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個骰子
- 01-11ajax實現(xiàn)頁面的局部加載
- 08-05DEDE織夢data目錄下的sessions文件夾有什
- 01-10C#中split用法實例總結
- 04-02jquery與jsp,用jquery
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文


