const 与 shared_ptr

const 与 shared_ptr

注意: 需要c++11的支持

const 与 shared_ptr 之间的组合不是很常见,最多的也只是在传递参数时:

shared_ptr<int> func(const shared_ptr<int> &ptr) {
	*ptr += 1;

	return ptr;
}

这里的 const shared_ptr<int> &ptr和常规的 const T &p一样,这里的const都是顶层const,也就是说我们不能改变ptr的地址,也不能改变p的值。

常规的内置指针和const有以下组合:

	constexptr int a = 5;  // a定义在函数体之外。
	
	const int *ptr1 = &a;             // 指向整型常量 的 指针
	int const *ptr2 = &a;             // 指向整型    的 常量指针
	const int const *ptr3 = &a;       // 指向整型常量 的 常量指针
	constexpr int *ptr4 = &a;         // 指向整型    的 常量指针
	constexpr const int *ptr5 = &a;   // 指向整型常量 的 常量指针

shared_ptr与const也有一下组合:

    int a = 5;

	const shared_ptr<int> ptr1 = make_shared<int>(a);        // 指向整型    的 常量智能指针
	shared_ptr<const int> ptr1 = make_shared<int>(a);        // 指向整型常量 的 智能指针
	const shared_ptr<const int> ptr3 = make_shared<int>(a);  // 指向整型常量 的 常量智能指针
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
对于const std::shared_ptr的赋值,根据引用中的信息,在C++中,std::shared_ptr对象是可以被赋值给const std::shared_ptr的。具体来说,有以下几个赋值运算符可用于const std::shared_ptr对象赋值: - const std::shared_ptr<T>& operator=(const std::shared_ptr<T>& r) noexcept; (1):将一个std::shared_ptr对象的值赋给另一个const std::shared_ptr对象。 - template <class Y> const std::shared_ptr<T>& operator=(const std::shared_ptr<Y>& r) noexcept; (1):将一个std::shared_ptr<Y>对象的值赋给const std::shared_ptr<T>对象。 - const std::shared_ptr<T>& operator=(const std::shared_ptr<T>&& r) noexcept; (2):将一个右值std::shared_ptr对象的值赋给const std::shared_ptr对象。 - template <class Y> const std::shared_ptr<T>& operator=(const std::shared_ptr<Y>&& r) noexcept; (2):将一个右值std::shared_ptr<Y>对象的值赋给const std::shared_ptr<T>对象。 - template <class Y> const std::shared_ptr<T>& operator=(std::auto_ptr<Y>&& r); (3):将一个右值std::auto_ptr<Y>对象的值赋给const std::shared_ptr对象。(在C++11中已弃用,在C++17中已移除) - template <class Y, class Deleter> const std::shared_ptr<T>& operator=(std::unique_ptr<Y,Deleter>&& r); (4):将一个右值std::unique_ptr<Y,Deleter>对象的值赋给const std::shared_ptr对象。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [c++11 智能指针 (std::shared_ptr)(一)](https://blog.csdn.net/qq_40788199/article/details/126695619)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *3* [c++11 智能指针 (std::shared_ptr)(二)](https://blog.csdn.net/qq_40788199/article/details/126695993)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值