C++中所謂數(shù)組引用,即指向數(shù)組的引用;
如:
1
2
|
int a[10] ; int (&b)[10] = a ; |
如果寫成:
1
2
|
int a[10] ; int * &b = a ; |
系統(tǒng)將會報錯: cannot convert from 'int [10]' to 'int *&'。
或許你會說在數(shù)組名不就是指向這個數(shù)組的一個指針嗎?題中a是int*類型的,b是指向int*的引用,按理應(yīng)該是正確的啊,為什么會報錯呢?這是因為編譯器對指向數(shù)組的引用檢查更加嚴(yán)格,需要檢查數(shù)組的維數(shù),在這里a被理解成指向10個int數(shù)組的指針int [10],對于引用也需要相應(yīng)的引用類型int (&)[10],即指向10個int數(shù)組的指針的引用。
c和c++中有一個“數(shù)組降價”問題。如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
#include <iostream> void test( char arr[100] ) { std::cout << sizeof (arr) << std::endl ; // 輸出 4 } int main() { char arr[100] = { 0 }; std::cout << sizeof (arr) << std::endl; // 輸出 100 test( arr ); return 0 ; } |
這段代碼的輸出是:
100
4
對于同樣的arr,一個輸出100,另一個輸出4。是因為void test( char arr[100] )中的arr被降價了。
void test( char arr[100] ) 中的arr被降階處理了,
void test( char arr[100] ) 等同于void test( char arr[] ), 也等同于void test( char* const arr ) 如果你原意,它甚至等同于void test( char arr[10] )
編譯器對數(shù)組的維數(shù)不作檢查。也就是說:
1
2
3
4
|
void test( char arr[100] ) { std::cout << sizeof (arr) << std::endl; } |
被降成
1
2
3
4
|
void test( char * const arr ) { std::cout << sizeof (arr) << std::endl; // 既然是char*,當(dāng)然輸出4 } |
這樣,即然不檢查數(shù)組的大小,對于需要保證數(shù)組大小的程序就會帶來問題。如何解決這個問題呢?可以用c++中的對數(shù)組的引用。
看下面這段代碼:
1
2
3
4
5
6
7
8
9
10
|
...... void test( const char (&arr)[100] ) { std::cout << sizeof (arr) << std::endl ; // 輸出 100 } ...... char arr[100] = { 0 }; std::cout << sizeof (arr) << std::endl; // 輸出 100 test( arr ); ...... |
這樣test就能接受100個char的數(shù)組,且只能接受大小為100的char數(shù)組。
如果:
1
2
|
char arr[20] = {0}; test( arr ) ; |
就會報錯
在C++中,對數(shù)組的引用可以直接傳遞數(shù)組名,因為數(shù)組的大小的信息已在形參里提供了。但是這樣一來我們只能固定數(shù)組的大小來用這個函數(shù)了。用模板加數(shù)組的引用可以解決這個問題,看如下代碼:
1
2
3
4
5
6
7
8
|
template < int sz> void test( char (&arr)[sz]) { for ( int i = 0; i < sz; i++ ) ......} char a[2] = { 0 }, b[15] = { 0 }; test(a); //ok test(b); //ok...... |
這樣解決了數(shù)組長度可變的問題,但也引入了新的問題:
(1)當(dāng)有多個不同的test調(diào)用時,會產(chǎn)生多份test代碼。而傳統(tǒng)的函數(shù)調(diào)用只有一份代,也調(diào)用的次數(shù)無關(guān)。
(2)由于這些代碼都是在編譯階段生成的,它需要知道引用數(shù)組的大小。所以這樣寫的函數(shù)顯然不能用指針變量作為函數(shù)的參數(shù),因此不能用這個函數(shù)處理動態(tài)分配的內(nèi)存區(qū)域,這樣的區(qū)域的大小是在運(yùn)行時確定的。