What does '&' do in a C++ declaration?

前端 未结 7 1403
后悔当初
后悔当初 2020-11-28 03:40

I am a C guy and I\'m trying to understand some C++ code. I have the following function declaration:

int foo(const string &myname) {
  cout << \"ca         


        
7条回答
  •  情书的邮戳
    2020-11-28 04:44

    #include
    using namespace std;
    int add(int &number);
    
    int main ()
    {
                int number;
                int result;
                number=5;
                cout << "The value of the variable number before calling the function : " << number << endl;
                result=add(&number);
                cout << "The value of the variable number after the function is returned : " << number << endl;
                cout << "The value of result : " << result << endl;
                return(0);
    }
    
    int add(int &p)
    {
                *p=*p+100;
                return(*p);
    }
    

    This is invalid code on several counts. Running it through g++ gives:

    crap.cpp: In function ‘int main()’:
    crap.cpp:11: error: invalid initialization of non-const reference of type ‘int&’ from a temporary of type ‘int*’
    crap.cpp:3: error: in passing argument 1 of ‘int add(int&)’
    crap.cpp: In function ‘int add(int&)’:
    crap.cpp:19: error: invalid type argument of ‘unary *’
    crap.cpp:19: error: invalid type argument of ‘unary *’
    crap.cpp:20: error: invalid type argument of ‘unary *’
    

    A valid version of the code reads:

    #include
    using namespace std;
    int add(int &number);
    
    int main ()
    {
                int number;
                int result;
                number=5;
                cout << "The value of the variable number before calling the function : " << number << endl;
                result=add(number);
                cout << "The value of the variable number after the function is returned : " << number << endl;
                cout << "The value of result : " << result << endl;
                return(0);
    }
    
    int add(int &p)
    {
                p=p+100;
                return p;
    }
    

    What is happening here is that you are passing a variable "as is" to your function. This is roughly equivalent to:

    int add(int *p)
    {
          *p=*p+100;
          return *p;
    }
    

    However, passing a reference to a function ensures that you cannot do things like pointer arithmetic with the reference. For example:

    int add(int &p)
    {
                *p=*p+100;
                return p;
    }
    

    is invalid.

    If you must use a pointer to a reference, that has to be done explicitly:

    int add(int &p)
    {
                        int* i = &p;
                i=i+100L;
                return *i;
    }
    

    Which on a test run gives (as expected) junk output:

    The value of the variable number before calling the function : 5
    The value of the variable number after the function is returned : 5
    The value of result : 1399090792
    

提交回复
热议问题