通过std :: vector进行迭代的面向对象方法? [英] Object Oriented way to iterate through a std::vector?
本文介绍了通过std :: vector进行迭代的面向对象方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个带有std :: vector子控件指针的类.出于明显的原因,我不希望该类的用户直接访问std :: vector.我想要的只是一种给调用者指针的方法. OO这样做的一种好方法是什么? (此函数将经常调用)
I have a class which has a std::vector of child control pointer. For obvious reasons, I do not want the user of the class to have direct access to the std::vector. All I would want is a way to give the caller the pointers. What would be a good OO way to do this? (this function will be called often)
谢谢
推荐答案
提供一个向向量返回const_iterator
的函数.添加一个将迭代器返回到向量的末尾也很有用.
Provide a function that returns a const_iterator
to the vector. It is also useful to add one to return the iterator to the end of the vector.
class MyClass {
public:
typedef vector<T>::const_iterator c_iter;
c_iter getBegin() const {return v.begin();}
c_iter getEnd() const {return v.end();}
// and perhaps if it's useful and not too invasive.
const T& getAt(int i) const {return v.at(i);}
//stuff
vector<T> v;
};
这篇关于通过std :: vector进行迭代的面向对象方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文