先决条件:C++中的指针与引用。
为了清楚地理解,在某些情况下,让我们比较一下“指针指向” VS“指针指向”的用法。
注意:在C和C++中都允许使用“指向指针的指针”,但是我们只能在C++中使用“指向指针的引用”。
将指针传递给函数
如果将指针作为参数传递给函数并试图进行修改,则对该指针所做的更改不会反映在该函数之外。这是因为仅将指针的副本传递给该函数。可以说“按指针传递”是按值传递指针。在大多数情况下,这不会带来问题。但是问题出在修改函数内部的指针时。您无需修改变量,而只需修改指针的副本,并且原始指针保持不变。
下面的程序说明了这一点:
#include
using namespace std;
int global_Var = 42;
// function to change pointer value
void changePointerValue(int* pp)
{
pp = &global_Var;
}
int main()
{
int var = 23;
int* ptr_to_var = &var;
cout << "Passing Pointer to function:" << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
changePointerValue(ptr_to_var);
cout << "After :" << *ptr_to_var << endl; // display 23
return 0;
}
输出:
Passing Pointer to function:
Before :23
After :23
将“指针指向指针”作为参数传递给函数
通过将指针的地址传递给函数而不是实际函数的副本,可以解决上述问题。为此,函数参数应接受“指向指针的指针”,如以下程序所示:
#include
using namespace std;
int global_var = 42;
// function to change pointer to pointer value
void changePointerValue(int** ptr_ptr)
{
*ptr_ptr = &global_var;
}
int main()
{
int var = 23;
int* pointer_to_var = &var;
cout << "Passing a pointer to a pointer to function " << endl;
cout << "Before :" << *pointer_to_var << endl; // display 23
changePointerValue(&pointer_to_var);
cout << "After :" << *pointer_to_var << endl; // display 42
return 0;
}
输出:
Passing a pointer to a pointer to function
Before :23
After :42
如何使用“引用指针”参数调用函数?
引用允许被调用函数修改调用者函数的局部变量。例如,考虑以下示例程序,其中fun()能够修改main()的局部变量x。
#include
using namespace std;
void fun(int &x) {
x = 20;
}
int main() {
int x = 10;
fun(x);
cout<<"New value of x is "<
输出:
New value of x is 20
下面的程序显示了如何将“引用指针”传递给函数:
#include
using namespace std;
int gobal_var = 42;
// function to change Reference to pointer value
void changeReferenceValue(int*& pp)
{
pp = &gobal_var;
}
int main()
{
int var = 23;
int* ptr_to_var = &var;
cout << "Passing a Reference to a pointer to function" << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
changeReferenceValue(ptr_to_var);
cout << "After :" << *ptr_to_var << endl; // display 42
return 0;
}
输出:
Passing a Reference to a pointer to function
Before :23
After :42
从函数返回指针
#include
using namespace std;
int global_var = 42;
// function to return a pointer
int* returnPointerValue()
{
return &global_var;
}
int main()
{
int var = 23;
int* ptr_to_var = &var;
cout << "Return a pointer from a function " << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
ptr_to_var = returnPointerValue();
cout << "After :" << *ptr_to_var << endl; // display 42
return 0;
}
输出:
Return a pointer from a function
Before :23
After :42
从函数返回引用
#include
using namespace std;
int global_var = 42;
// function to return reference value
int& ReturnReference()
{
return global_var;
}
int main()
{
int var = 23;
int* ptr_to_var = &var;
cout << "Returing a Reference " << endl;
cout << "Before :" << *ptr_to_var << endl; // display 23
ptr_to_var = &ReturnReference();
cout << "After :" << *ptr_to_var << endl; // display 42
return 0;
}
输出:
Returing a Reference
Before :23
After :42
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。