为什么不能在C ++中的STL映射中存储引用? [英] Why Can't I store references in an STL map in C++?
问题描述
我强调引用不是指针,而是对象的别名。但是,我仍然不明白这对于我来说意味着什么是一个程序员,即什么是引用下面?
I undersatnd 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.
我知道我需要停止思考引用作为指针的句法su,只是不确定如何:/
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 ++中的STL映射中存储引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!