What does *& mean in a function parameter

前端 未结 5 1085
春和景丽
春和景丽 2020-12-03 14:43

If I have a function that takes int *&, what does it means? How can I pass just an int or a pointer int to that function?

function(int *&am         


        
相关标签:
5条回答
  • 2020-12-03 15:20

    It's a reference to a pointer to an int. This means the function in question can modify the pointer as well as the int itself.

    You can just pass a pointer in, the one complication being that the pointer needs to be an l-value, not just an r-value, so for example

    int myint;
    function(&myint);
    

    alone isn't sufficient and neither would 0/NULL be allowable, Where as:

    int myint;
    int *myintptr = &myint;
    function(myintptr);
    

    would be acceptable. When the function returns it's quite possible that myintptr would no longer point to what it was initially pointing to.

    int *myintptr = NULL;
    function(myintptr);
    

    might also make sense if the function was expecting to allocate the memory when given a NULL pointer. Check the documentation provided with the function (or read the source!) to see how the pointer is expected to be used.

    0 讨论(0)
  • 2020-12-03 15:30

    This is a reference to a pointer to int - you would have to pass in the address of an int to this function, and be aware that the function could change the pointer through the reference.

    Dumb example:

    void func(int*& iref) 
    {
        iref = new int;
    }
    
    int main()
    {
        int i(0);
        int* pi(&i);
    
        func(pi);
        // pi no longer equal to &i
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-03 15:38

    It means a reference to a pointer to an int. In other words, the function can change the parameter to point to something else.

    To pass a variable in, just pass an int*. As awoodland points out, what's passed in must be an l-value.

    Edit:

    To build on awoodland's example:

    #include <iostream>
    
    void foo(int*& var)
    {
        delete var;
        var = new int;
    }
    
    int main(int argc, char* argv[])
    {
        int* var = NULL;
    
        std::cout << var << std::endl;
    
        foo(var);   // this function can/will change the value of the pointer
    
        std::cout << var << std::endl;
    
        delete var;
    
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-03 15:39

    Simply: a reference to a pointer.

    In C, without references, the traditional way to "relocate" a pointer, is to pass a pointer to a pointer:

    void c_find(int** p, int val); /* *p will point to the node with value 'val' */
    

    In C++, this can be expressed by the reference syntax, to avoid the awkward double dereference.

    void cpp_find(int*& p, int val); // p will point to the node with value 'val'
    
    0 讨论(0)
  • 2020-12-03 15:39

    function takes a single parameter, mynumber which is a reference to a pointer to an int.

    This is useful when you need to pass a pointer to a function, and that function might change the pointer. For example, if you function is implemented like this:

    function(int*& mynumber)
    {
      if( !mynumber )
        mynumber = new int;
      *mynumber = 42;
    }
    

    ...Then something like this might happen in the calling code:

    int main()
    {
      int* mynumber = 0;
      function(mynumber); // function will change what "mynumber" points to
      cout << *mynumber;
      return 0;
    }
    
    0 讨论(0)
提交回复
热议问题