如何在所有函数调用中保持作为参数传递的指针中存储的地址的一致性



我可以通过引用传递任何参数,任何函数调用中对该变量的更改也会反映在其他函数调用中。

同样,如果我想在所有函数调用中保持指针中存储的地址一致,这样它的行为就像我们通过引用传递指针一样。

您也可以按照注释中的建议通过引用传递指针。

#include <iostream>
void f(int * & p) {
std::cout << p << std::endl;
}
int main() {
int x;
int * p = &x;
std::cout << p << std::endl; // This prints the same...
f(p);                        // ... as this.
}

最新更新