C++ *&取消引用运营商的地址? [英] c++ *& dereference address of operator?

查看:57
本文介绍了C++ *&取消引用运营商的地址?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在最近的数据结构类作业中,我们希望使用这个*&"在函数参数列表中.我无法找到有关为什么存在这种情况的信息,甚至无法找到它的作用.使用它是标准做法吗?

In a recent assignment for a data structures class, we we're expected to use this "*&" in a function parameters list. I am having trouble finding info on why this exists, and even what it does. Is it standard practice to use this?

这是一个代码片段

template <class T>
Thing <T> * action(const T &dataIn, Thing <T> *& thingIn)
{
    if(thingIn == NULL)
    {
        Node <T> *newThing = new Thing <T> (dataIn);

        newThing->ptr = thingIn;
        thingIn= newThing ;

        return(newThing );
    }
}

我想我的问题的另一部分是,为什么使用*&"你就不能直接发送指针吗?

I guess another part of my question is, why use "*&" at all couldn't you just send the pointer?

推荐答案

fun(int x)

fun 需要一个 int 值

fun takes an int value

好玩(int* x)

fun 按值获取 int 指针

fun takes an int pointer by value

有趣(int& x)

fun(int& x)

fun 通过引用获取 int

fun takes an int by reference

有趣(int*& x)

fun(int*& x)

fun 通过引用获取一个 int 指针 - 调用者看到的指向 x 的指针的值可以被 fun 改变

fun takes an int pointer by reference - the value of the pointer to x as seen by the caller can be changed by fun

这篇关于C++ *&amp;取消引用运营商的地址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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