Professional Documents
Culture Documents
References To Pointers
References To Pointers
References to Pointers
Visual Studio 2015
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
References to pointers can be declared in much the same way as references to objects. Declaring a reference to a pointer
yields a modifiable value that is used like a normal pointer.
Example
The following code samples illustrate the difference between using a pointer to a pointer and a reference to a pointer.
Functions Add1 and Add2 are functionally equivalent (although they are not called the same way). The difference is that Add1
uses double indirection whereas Add2 uses the convenience of a reference to a pointer.
// references_to_pointers.cpp
// compile with: /EHsc
#include <iostream>
#include <string>
// STL namespace
using namespace std;
enum {
sizeOfBuffer = 132
};
https://msdn.microsoft.com/en-us/library/1sf8shae(d=printer).aspx 1/3
10/07/2017 References to Pointers
cerr << "\nInput is from stdin.\n";
return 1;
}
if ( strlen( szBuf ) ) {
switch ( *argv[1] ) {
// Method 1: Use double indirection.
case '1':
Add1( &btRoot, szBuf );
break;
// Method 2: Use reference to a pointer.
case '2':
Add2( btRoot, szBuf );
break;
default:
cerr << "Illegal value '"
<< *argv[1]
<< "' supplied for add method.\n"
<< "Choose 1 or 2.\n";
return -1;
}
}
}
// Display the sorted list.
PrintTree( btRoot );
}
https://msdn.microsoft.com/en-us/library/1sf8shae(d=printer).aspx 2/3
10/07/2017 References to Pointers
Output
Usage: Refptr [1 | 2]
where:
1 uses double indirection
2 uses a reference to a pointer.
See Also
References
© 2017 Microsoft
https://msdn.microsoft.com/en-us/library/1sf8shae(d=printer).aspx 3/3