投射矢量到向量

发布于 2024-09-02 01:13:04 字数 396 浏览 6 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

很糊涂小朋友 2024-09-09 01:13:04

如果您有一个 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 a const vector<const int> to achieve those semantics.

此岸叶落 2024-09-09 01:13:04

为什么即使 T 可以转换为 const,vector 也无法正确转换为 vector

无论是常量性还是继承性,这都是编程中常见的反复出现的问题(派生对象的容器不能转换为基对象的容器,即使所包含的元素本身可以) 问题是它们中的每一个都可以逐个元素地转换,但容器本身不能在不破坏类型系统的情况下进行转换。

如果你被允许做vector<常量 T > &vr = my_vector_of_T,那么您将被允许通过 vr 添加元素,并且根据定义,这些元素将是常量。但同时这些相同的元素将在 my_vector_of_T 中被别名为非常量元素,并且可以通过该接口进行修改,从而破坏类型系统中的常量性。

在将 vector 转换为 vector 的特定情况下,除了添加元素之外,您很可能不会注意到真正奇怪的效果到 vector 并查看 constant 元素如何随时间变化,但仍然记住给定两个相关类型 T1T2 存在关系,在大多数情况下,尝试将相同的关系应用于 T1T2 的容器将破坏类型系统。

On why a vector<T> cannot be correctly converted to a vector<const T> even if T can be converted to const 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 through vr, and those elements would be constant by definition. But at the same time those same elements would be aliased in my_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 a vector<const int>, chances are that you would not notice really weird effects --besides adding an element to a vector<const int> and seeing how the constant element changes in time, but still remember that given two related types T1 and T2 for which a relation exists, in most cases trying to apply the same relationship to containers of T1 and T2 will break the type system.

抱猫软卧 2024-09-09 01:13:04

除了 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.

夜清冷一曲。 2024-09-09 01:13:04

你可以像这样强制转换:

b = reinterpret_cast<const std::vector<const int>*>(&a);

但我认为你不应该这样做,因为它不能保证工作,只能编译

you can force conversion like this:

b = reinterpret_cast<const std::vector<const int>*>(&a);

but I do not think you should do this, since it is not guaranteed to work, only to compile

許願樹丅啲祈禱 2024-09-09 01:13:04

编译器决定阻止此操作。然而,我们知道这是安全的,所以也许我们可以欺骗它:

const vector<const int>* b = (const vector<const int>* )(void *)(&a);

The compiler decides to block this. However, we know this is safe so maybe we can fool it:

const vector<const int>* b = (const vector<const int>* )(void *)(&a);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文