Why Can#39;t I store references in a `std::map` in C++?(为什么我不能在 C++ 的 `std::map` 中存储引用?)
问题描述
我知道引用不是指针,而是对象的别名.但是,作为程序员,我仍然不明白这对我来说究竟意味着什么,即引擎盖下的引用是什么?
I understand that references are not pointers, but an alias to an object. However, I still don't understand what exactly this means to me as a programmer, i.e. what are references under the hood?
我认为理解这一点的最好方法是理解为什么我不能在地图中存储参考.
I think the best way to understand this would be to understand why it is I can't store a reference in a map.
我知道我需要停止将引用视为对指针的句法支持,只是不知道如何:/
I know I need to stop thinking of references as syntactic suger over pointers, just not sure how to :/
推荐答案
按照我的理解,引用在幕后实现为指针.不能将它们存储在地图中的原因纯粹是语义;您必须在创建引用时对其进行初始化,之后便无法再更改它.这与地图的工作方式不符.
They way I understand it, references are implemented as pointers under the hood. The reason why you can't store them in a map is purely semantic; you have to initialize a reference when it's created and you can't change it afterward anymore. This doesn't mesh with the way a map works.
这篇关于为什么我不能在 C++ 的 `std::map` 中存储引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我不能在 C++ 的 `std::map` 中存储引用?
基础教程推荐
- 随机插入/删除的综合向量与链表基准 2022-01-01
- 为什么我们不能使用“虚拟继承"?在 COM 中? 2022-01-01
- 与 CAS 的原子交换(使用 gcc 同步内置函数) 2022-01-01
- 提升 ASIO 流缓冲 2021-01-01
- 将不可复制的闭包对象传递给 std::function 参数 2021-01-01
- 如何部分禁用 cmake C/C++ 自定义编译器检查 2021-01-01
- 如何更改 SysDateTimePick32 或 CDateTimeCtrl 的背景颜色? 2022-01-01
- c++ STL设置差异 2022-01-01
- C++:获取传递给函数的多维数组的行大小 2021-01-01
- 如何在 C++ 中正确使用命名空间? 2022-01-01
