PassByVal、PassByRef 和 PassByConstRef 的比较 std::shared_ptr (什么
Comparison of PassByVal, PassByRef, and PassByConstRef of std::shared_ptr (What can(cannot) be changed)
#include <iostream>
#include <string>
#include <memory>
std::shared_ptr<std::string> ptrStr(new std::string("One"));
// increases the reference count to std::string("One") by one after entering the function
// decreases the reference count to std::string("One") by one before exiting the function
void PassByValue(std::shared_ptr<std::string> msg)
{
msg->clear(); // clears the string pointed by ptrStr
msg.reset(new std::string("hello world")); // doesn't change the value of ptrStr
// because it only changes a local copy of ptrStr
// which will be destroyed when the function is returned.
// Note the msg, the local copy of ptrStr, points to the same
// resource where ptrStr points to.
}
// doesn't affect the reference count
void PassByReference(std::shared_ptr<std::string> &msg)
{
msg->clear(); // clears the string pointed by ptrStr
msg.reset(new std::string("hello world")); // does change the value of ptrStr
}
// doesn't affect the reference count
void PassByConstReference(const std::shared_ptr<std::string>& msg)
{
msg->clear(); // clears the string pointed by ptrStr
msg.reset(new std::string("hello world")); // compilation errors
}
int main(int argc, char* argv[])
{
// Each time only one of the following three lines is executed
//PassByValue(ptrStr);
//PassByReference(ptrStr);
//PassByConstReference(ptrStr)
std::cout << *ptrStr << std::endl;
return 0;
}
注意:我不是在争论哪种传递机制是好是坏。我只想了解std::shared_ptr
传递机制不同的后果,只想关注传递std::shared_ptr
作为函数参数。
问题1>如果我错了,请帮助阅读评论并纠正我
问题2>是不是真的,不管选择哪种机制,总能改变std::shared_ptr
所指的资源?
问题 3> std::shared_ptr
是否与其他函数参数相似,并且在 PassByValue、PassByReference 和 PassByConstReference 方面具有类似的行为。
谢谢
问题1>如果我错了,请帮助阅读评论并纠正我
根据我的理解,你的评论是正确的。
问题2> 不管选择哪种机制,你总是可以改变std::shared_ptr所指向的资源,这是真的吗?
嗯,有点。您始终可以从std::shared_pointer
中检索原始指针。当您将其作为const
传递时,您将获得一个const
原始指针。如果要操作指向对象的内容,则会出现编译错误。但是有一些方法可以将const
指针转换为普通指针,例如使用 const_cast
.
问题 3>std::shared_ptr 与其他函数参数相似,并且在 PassByValue、PassByReference 和 PassByConstReference 方面具有类似的行为,这是真的吗?
从C++的角度来看,std::shared_pointer
s只是与其他任何类型一样的类型,所以不,关于参数传递的方法,这里没有区别。
- 您的所有评论都是完全正确的。
- 你可以路过
std::shared_ptr<std::string const>
.在这种情况下,shared_ptr
指向的资源是不可更改的。 - 我不确定你在问什么,但是通过参数的规则没有
std::shared_ptr
的特殊情况.
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 在以唯一ptr为值的C++映射中,动态内存何时会被销毁
- 将 ptr 传递给 ptr 到 A 作为参数传递给 A 的函数是不好的做法吗?
- 为共享 ptr 向量实现复制 c'tor?
- 字符和整数中 **(ptr+1) 的值差异
- C++:在不中断共享的情况下通过引用传递共享 PTR?
- 如何将派生类从基 ptr 分配给 nlohmann::json
- 引用 std::shared:ptr 以避免引用计数
- 为什么我不能在不进行任何转换的情况下将浮点数放入任何类型的 ptr 中?
- 在调用函数时,ptr** 和 ptr*& 之间是否有区别,或者首选C++?
- 另一种类型的智能ptr,比如具有弱refs的unique_ptr
- 尝试打印出 *ptr++ 的值,以了解它是如何工作的
- 如何控制共享 ptr 引用计数?
- dopen():不以 root 身份运行时"failed to map segment from shared object"
- C++中的指针否定 (!ptr == NULL)
- 从const ptr*转换为ptr*时出现问题
- 无法使用 libtool 将 -shared 参数传递给 g++
- boost::shared_ptr和std::shared-ptr的同居
- 我可以用std::shared_ptr而不是boost::shared-ptr构建boost库吗
- shared-ptr-C++shared_ptr与unique_ptr用于资源管理