2011-04-26 64 views
37

我在函數聲明中多次發現了這些符號,但我不知道它們的含義。*&和**的含義在C++中

實施例:

void raccogli_dati(double **& V, double **p, int N) { 
    int ultimo = 3; 
    V = new double * [N/2]; 
    for(int i=0; i < N/2; i++) { 
    V[i] = new double[N/2], std :: clog << "digita " << N/2 - i 
       << " valori per la parte superiore della matrice V: "; 
    for(int j=i; j < N/2; j++) 
     std :: cin >> V[i][j], p[ultimo++][0] = (V[i][j] /= sqrt(p[i][0]*p[j][0])); 
    } 
    for(int i=1; i < N/2; i++) 
    for(int j=0; j < i; j++) 
     V[i][j] = V[j][i]; 
} 
+0

在實際的代碼或一本書嗎?對我來說,它只是一個普通功能的符號。一個返回單個指針,另一個返回指針。 – 2011-04-26 11:48:25

+0

這些是指針的引用。 – 2011-04-26 11:49:34

+0

像這樣:void raccogli_dati(double **&V,double ** p,int N) { \t int ultimo = 3; \t V = new double * [N/2]; \t對(INT I = 0; I > V [i] [j], \t \t P [阿爾提莫++] [0] =(V [I] [j]/= sqrt(p [i] [0] * p [j] [0])); } \t的for(int i = 1; I sdffadsf 2011-04-26 11:49:34

回答

54

也就是說通過引用取參數。因此,在第一種情況下,您通過引用獲取指針參數,因此無論您對指針值做何修改都會反映到該函數之外。其次是類似於第一個,唯一的區別是它是雙指針。看到這個例子:

void pass_by_value(int* p) 
{ 
    //Allocate memory for int and store the address in p 
    p = new int; 
} 

void pass_by_reference(int*& p) 
{ 
    p = new int; 
} 

int main() 
{ 
    int* p1 = NULL; 
    int* p2 = NULL; 

    pass_by_value(p1); //p1 will still be NULL after this call 
    pass_by_reference(p2); //p2 's value is changed to point to the newly allocate memory 

    return 0; 
} 
9

這是通過引用而不是按值傳遞指針。這例如允許改變函數中的指針(而不是指向的對象),使得調用代碼看到改變。

比較:

void nochange(int* pointer) //passed by value 
{ 
    pointer++; // change will be discarded once function returns 
} 

void change(int*& pointer) //passed by reference 
{ 
    pointer++; // change will persist when function returns 
} 
+0

感謝提到持久性的差異 – 2015-12-16 16:14:09

10

首先是一個指針的引用,第二個是一個指針的引用的指針。另見關於how pointers and references differ的常見問題。

void foo(int*& x, int**& y) { 
    // modifying x or y here will modify a or b in main 
} 

int main() { 
    int val = 42; 
    int *a = &val; 
    int **b = &a; 

    foo(a, b); 
    return 0; 
} 
+1

我知道我不應該真的,但我upvoted這個答案只是因爲它包含一個指針指向一個指向一個指針的指針-生命的意義。不知何故,我認爲我們離深思想還有幾年的時間。 – corlettk 2011-04-26 12:18:31

5

int*是一個指向int。然後int*&必須是對指向int的指針的引用。同樣,int**是指向指向int的指針,則int ** &必須是指向指向int的指針的指針。

+0

我明白他們的字面意思,你能寫一個例子來說明其中更有效嗎? – Sheldon 2017-06-05 14:19:20

3

理解這些詞語讓我們來看看幾件事情:

typedef double Foo; 
void fooFunc(Foo &_bar){ ... } 

所以這是經過雙重引用。

typedef double* Foo; 
void fooFunc(Foo &_bar){ ... } 

現在它傳遞一個指向雙引用的指針。

typedef double** Foo; 
void fooFunc(Foo &_bar){ ... } 

最後,它傳遞一個指向double引用的指針。如果你按照這樣的typedef來思考,你會明白&和* plus的正確順序。

3

*&表示通過引用接收指針。這意味着它是傳遞參數的別名。所以,它會影響傳遞參數。

#include <iostream> 
using namespace std; 

void foo(int *ptr) 
{ 
    ptr = new int(50); // Modifying the pointer to point to a different location 
    cout << "In foo:\t" << *ptr << "\n"; 
    delete ptr ; 
} 

void bar(int *& ptr) 
{ 
    ptr = new int(80); // Modifying the pointer to point to a different location 
    cout << "In bar:\t" << *ptr << "\n"; 
    // Deleting the pointer will result the actual passed parameter dangling 
} 
int main() 
{ 
    int temp = 100 ; 
    int *p = &temp ; 

    cout << "Before foo:\t" << *p << "\n"; 
    foo(p) ; 
    cout << "After foo:\t" << *p << "\n"; 

    cout << "Before bar:\t" << *p << "\n"; 
    bar(p) ; 
    cout << "After bar:\t" << *p << "\n"; 

    delete p; 

    return 0; 
} 

輸出:

Before foo: 100 
In foo: 50 
After foo: 100 
Before bar: 100 
In bar: 80 
After bar: 80