为什么我们在 C++ 中按引用传递指针?
如果我们需要修改指针,而不是指针所指向的对象,我们按引用传递指针。
下面是按引用传递指针的一个示例 −
示例
#include <iostream>
using namespace std;
void Decrement( int*& d ) {
--d;
}
int main( void ) {
int a = 26;
int* ptr = &a; // pointer to pass
// print before decrement
cout<<"Before: "<< ptr << endl;
Decrement( ptr);
// print after increment
cout<<"After: " << ptr;
return 0;
}输出
Before: 0x6ffe3c After: 0x6ffe38
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP