cast stl :: vector包含指向包含常量指针的stl :: vector的指针 [英] cast stl::vector containing pointers to stl::vector containing constant pointers

查看:407
本文介绍了cast stl :: vector包含指向包含常量指针的stl :: vector的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的类:

class SomeInfo
{
private:
    std::vector<someObj *> _myVector;

public:
    const std::vector<const someObj*>& getInfoVector() const
    {
        return _myVector;
    }
};

当我尝试用gcc 4.1.2编译时,它给我以下错误:

When I try to compile with gcc 4.1.2, it gives me the following error:

error: invalid initialization of reference of type 
‘const std::vector<const someObj*, std::allocator<const someObj*> >&’ 
from expression of type 
‘const std::vector<someObj*, std::allocator<someObj*> >

如果我删除'someObj *'前面的'const',那么它会编译,不想返回带有非常量指针的向量,因为我不想让它们引用的对象从SomeInfo类的外部改变。

If I remove the 'const' in front of 'someObj *', then it compiles, but I don't want to return vector with non-constant pointers, because I don't want the objects referenced by them to be changed from outside my SomeInfo class. What would you do in this situation?

推荐答案

在这种情况下,我会做的是忘记返回 vector (或其引用)。调用者不能进行任何更改,因此不需要看到任何特定的容器。其他答案解释为什么你不能引用你的向量,如果它是一个向量 const someObj * 。对于这个问题,如果将来你改变你的类使用 deque 而不是向量,你不能返回一个对它的引用,好像它是一个向量。因为调用者只需要访问元素,让我们给它们:

What I would do in this situation is forget about returning a vector (or reference thereto). The caller can't make any changes, so doesn't need to see any particular container. Other answers explain why you can't refer to your vector as if it were a vector of const someObj*. For that matter, if in future you change your class to use a deque instead of a vector you can't return a reference to that as if it were a vector either. Since callers just need access to the elements, let's give them that:

const someObj *getInfoAt(size_t n) const {
    return _myVector.at(n);
}

const_info_iterator getInfoBegin() const {
    return _myVector.begin();
}

const_info_iterator getInfoEnd() const {
    return _myVector.end();
}

size_t getInfoSize() const {
    return _myVector.size();
}

// plus anything else they need.

const_info_iterator 是类的typedef有点烦人的写。它必须包装 std :: vector< someObj *> :: const_iterator 和const-ify类型 operator * boost :: transform_iterator 可能是有用的,或者从头开始写不好。双向迭代器确实需要最多的运算符重载。

const_info_iterator is a typedef to a class that's slightly annoying to write. It has to wrap a std::vector<someObj *>::const_iterator and const-ify the type of operator*. boost::transform_iterator might be useful, or it's not all that bad to write from scratch. Bidirectional iterators do require the most operator overloads, though.

现在,如果调用者真的想要一个向量 const someObj * ,然后使用我的界面,他们可以很容易地得到任何特定时刻的快照:

Now, if the caller really wants a vector of const someObj*, then using my interface they can easily get a snapshot at any particular moment:

std::vector<const someObj*> mycopy(myinfo.getInfoBegin(), myinfo.getInfoEnd());

他们不能拥有的是向量 std :: vector 不断反映对其他类型的某些其他向量不要这样做。

What they can't have is a vector that continually reflects changes made to some other vector of a different type somewhere else -- std::vector just doesn't do that.

这篇关于cast stl :: vector包含指向包含常量指针的stl :: vector的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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