加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – 将智能指针传递给参考指针参数的函数

发布时间:2020-12-16 10:24:46 所属栏目:百科 来源:网络整理
导读:如何将智能ptr传递给参考指针作为参数的函数? smart_ptrT val; // I have this smart pointer// And I want to pass it to this function,so that this function will fill the smart pointer with proper valuevoid Foo(T* sth){ sth = memoryAddress;} 编
如何将智能ptr传递给参考指针作为参数的函数?

smart_ptr<T> val; // I have this smart pointer

// And I want to pass it to this function,so that this function will fill the smart pointer with proper value
void Foo(T*& sth)
{
    sth = memoryAddress;
}

编辑
现在我懂了.谢谢大家的所有答案!

解决方法

简单的答案是你做不到.而智能指针 几乎可以肯定在内部包含一个T *,聪明 指针强制执行各种不变量,其中许多可能是 如果你可以改变这个指针而不通过就破了 用户界面.唯一的解决方案是调用该函数 使用原始指针,然后使用原始指针进行初始化 智能指针,只要你确定指针 你得到满足智能指针的要求(例如 由新操作符分配).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读