投射矢量到向量
我有一个 vector
类型的成员变量(其中 T 是自定义类,但也可以是 int。) 我有一个函数,我想从中返回指向该向量的指针,但我不希望调用者能够更改该向量或其项目。所以我希望返回类型为 const vector
我尝试过的转换方法都不起作用。编译器一直抱怨 T 与 const T 不兼容。
这里有一些代码演示了我正在尝试做的事情的要点;
vector<int> a;
const vector<const int>* b = (const vector<const int>* ) (&a);
这段代码不适合我编译。
提前致谢!
I have a member variable of type vector<T>
(where is T is a custom class, but it could be int as well.)
I have a function from which I want to return a pointer to this vector, but I don't want the caller to be able to change the vector or it's items. So I want the return type to be const vector<const T>*
None of the casting methods I tried worked. The compiler keeps complaining that T is not compatible with const T.
Here's some code that demonstrates the gist of what I'm trying to do;
vector<int> a;
const vector<const int>* b = (const vector<const int>* ) (&a);
This code doesn't compile for me.
Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您有一个 const vector,则无法修改容器,也无法修改容器中的任何元素。您不需要
const vector
来实现这些语义。If you have a
const vector<int>
you cannot modify the container, nor can you modify any of the elements in the container. You don't need aconst vector<const int>
to achieve those semantics.为什么即使
T
可以转换为const,
vector
也无法正确转换为vector
。无论是常量性还是继承性,这都是编程中常见的反复出现的问题(派生对象的容器不能转换为基对象的容器,即使所包含的元素本身可以) 问题是它们中的每一个都可以逐个元素地转换,但容器本身不能在不破坏类型系统的情况下进行转换。
如果你被允许做
vector<常量 T > &vr = my_vector_of_T
,那么您将被允许通过vr
添加元素,并且根据定义,这些元素将是常量。但同时这些相同的元素将在 my_vector_of_T 中被别名为非常量元素,并且可以通过该接口进行修改,从而破坏类型系统中的常量性。在将
vector
转换为vector
的特定情况下,除了添加元素之外,您很可能不会注意到真正奇怪的效果到vector
并查看 constant 元素如何随时间变化,但仍然记住给定两个相关类型T1
和T2
存在关系,在大多数情况下,尝试将相同的关系应用于T1
和T2
的容器将破坏类型系统。On why a
vector<T>
cannot be correctly converted to avector<const T>
even ifT
can be converted toconst T
This is a common recurring problem in programming whether it is with constness or inheritance (a container of derived object cannot be converted to a container of base objects, even if the contained elements themselves can). The problem is that element by element each one of them can be converted, but the container itself cannot without breaking the type system.
If you were allowed to do
vector< const T > &vr = my_vector_of_T
, then you would be allowed to add elements throughvr
, and those elements would be constant by definition. But at the same time those same elements would be aliased inmy_vector_of_T
as non-const elements and could be modified through that interface, breaking constness in the typesystem.In the particular case of a
vector<int>
being converted to avector<const int>
, chances are that you would not notice really weird effects --besides adding an element to avector<const int>
and seeing how the constant element changes in time, but still remember that given two related typesT1
andT2
for which a relation exists, in most cases trying to apply the same relationship to containers ofT1
andT2
will break the type system.除了 James 关于如何执行此操作的回答之外,您还应该注意 const int 不是放入任何标准容器的有效类型,因为它不可分配。
In addition to James's answer about how to do it you should note that
const int
is not a valid type to put into any standard container since it is not assignable.你可以像这样强制转换:
但我认为你不应该这样做,因为它不能保证工作,只能编译
you can force conversion like this:
but I do not think you should do this, since it is not guaranteed to work, only to compile
编译器决定阻止此操作。然而,我们知道这是安全的,所以也许我们可以欺骗它:
The compiler decides to block this. However, we know this is safe so maybe we can fool it: