C ++函数参数以指针的地址作为参数。这是如何使用?它是什么? [英] C++ function parameter takes an address of a pointer as an argument. How is this used? What is it for?

查看:282
本文介绍了C ++函数参数以指针的地址作为参数。这是如何使用?它是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

想象一下这样的函数:

 函数(Human *& human){
// Implementation
}

是什么?它会用于什么?如何区别只是传递一个指针或引用?

解决方案

p>它就像一个双指针。



例如,human可以指向Jeff,而函数可以指向修改它以指向Ann。

  Human ann(Ann); 

void function(Human *& human)
{
human =& ann;
}

int main()
{
人类jeff(Jeff);
Human * p =& jeff;
function(p); // p now point to ann
return 0;
}


Imagine a function like this:

 function(Human *&human){
      // Implementation
 }

Can you explain what exactly a *& is? And what would it be used for? How is different than just passing a pointer or a reference? Can you give a small and explanatory sample?

Thank you.

解决方案

It is like a double pointer. You're passing the pointer by reference allowing the 'function' function to modify the value of the pointer.

For example 'human' could be pointing to Jeff and function could modify it to point to Ann.

Human ann("Ann");

void function(Human *& human)
{
    human = &ann;
}

int main()
{
    Human jeff("Jeff");
    Human* p = &jeff;
    function(p);  // p now points to ann
    return 0;
}

这篇关于C ++函数参数以指针的地址作为参数。这是如何使用?它是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆