如何返回const std :: vector< Object * const&gt ;? [英] How-to return a const std::vector<Object *const>?

查看:137
本文介绍了如何返回const std :: vector< Object * const&gt ;?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有容器(包含指针)的类作为成员:

I have a class with a container (containing pointer) as a member:

MyClass{
private:
   std::vector<MyObject*> _VecMyObjs;
public:
   const std::vector<MyObject* const> GetVecMyObj();
}

现在我尝试实现GetVecMyObj()。这是我想出的...

Now I try to implement GetVecMyObj(). Here is what I came up with...

const vector<MyObject *const> ACI_CALL MyClass::GetVecMyObjs()
{
   const vector<MyObject *const> VecMyObjs;
   VecMyObjs.assign( _VecMyObjs.begin(), _VecMyObjs.end());
   return VecMyObjs;
}

但是编译器当然警告我,我使用了assign-function在常量对象上。有一个更好的方法吗?我的意思是,我当然不希望VecMyObjs在课程之外更改VecMyObj。在没有编译器警告的情况下如何实现?

But of course the compiler is warning me, that I use the assign-function on a const-Object. Is there a better way to do this? I mean, I don't want VecMyObjs to change VecMyObj outside of the class, of course. How can I achieve that without a compiler warning?

编辑:
好​​的。谢谢大家。现在就像这样:

Okay. Thank you everybody. It's now like this:

const vector<MyObject *const> ACI_CALL MyClass::GetVecMyObjs()
{
   const vector<MyObject *const> VecMyObjs;
   VecMyObjs.assign( _VecMyObjs.begin(), _VecMyObjs.end());
   return VecMyObjs;
}

但是,我无法绕过分配功能,对吗?例如。如果我希望所有内容保持不变,则强制转换原始字符将不起作用。

But, I can't get around the assign-function, right? E.g. casting the "original" doesn't work, if I want "everything" to be constant.

推荐答案

我不确定 std :: vector< MyObject * const> (常量指针的向量)确实是您想要的:我相信您的意思是 std :: vector< MyObject const *> (指向常量对象的指针的向量)。

I'm not sure std::vector<MyObject * const> (vector of constant pointers) is really what you want : I believe you mean std::vector<MyObject const *> (vector of pointer to constant objects).


  1. 第一级常数( pointer 常数)的定义自然是通过在向量上返回常量引用来实现的。从const向量只能获取 const_iterator ,因此可以保证不会修改指针(但可以修改 pointees )。

  1. The "first level" of constness (pointer constness) is naturally achieved by returning a constant reference on the vector. Only const_iterator can be obtained from a const vector, so you have a guarantee that the pointers won't be modified (but pointees can be).

更难以获得二级常量( pointee 常量)。要么返回其他人已经指出的vector的新实例:

The "second level" of constness (pointee constness) is harder to obtain. Either return a new instance of a vector as already pointed out by others :

return std::vector<const MyObject *>(_VecMyObjs.begin(), _VecMyObjs.end());

或者,如果适用,尝试查看提升指针容器库(最著名的是 ptr_vector )可提供正确的常数传播:

Or, if applicable, try to look into the Boost Pointer Container library (and most notably ptr_vector) which offers, among other things, correct constness propagation :


传播常量,使一个
无法通过
const_iterator修改对象。

Propagates constness such that one cannot modify the objects via a const_iterator.


您必须了解,在向量上返回const引用可以保证不能对其进行修改(不能插入,删除或修改其值) 。因此,在大多数情况下,返回 const std :: vector< T> & 是解决方法,因为如果不涉及任何复制。这里的问题实际上是特定于指针容器的,在这些容器中,值的恒定性不能提供指针的恒定性。

You have to understand that returning a const reference on a vector guarantees that it cannot be modified (no insertion, deletion, or modification of its value). So, in most cases, returning a const std::vector<T> & is the way to go because if does not involve any copying. The issue here is really specific to container of pointers, where constness of the values does not provide constness of the pointees.

这篇关于如何返回const std :: vector&lt; Object * const&gt ;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆