如何暴露迭代器,而不暴露使用的容器? [英] How can I expose iterators without exposing the container used?
问题描述
我一直在使用C#一段时间,而回到C ++是一个头痛。我试图从C#中获得一些我的实践从C到C ++,但我发现一些阻力,我很高兴接受你的帮助。
I have been using C# for a while now, and going back to C++ is a headache. I am trying to get some of my practices from C# with me to C++, but I am finding some resistance and I would be glad to accept your help.
我想为这样的类公开迭代器:
I would like to expose an iterator for a class like this:
template <class T>
class MyContainer
{
public:
// Here is the problem:
// typedef for MyIterator without exposing std::vector publicly?
MyIterator Begin() { return mHiddenContainerImpl.begin(); }
MyIterator End() { return mHiddenContainerImpl.end(); }
private:
std::vector<T> mHiddenContainerImpl;
};
我在尝试一个没有问题的东西吗?我应该只是typedef std :: vector< T> :: iterator?我希望只依赖于迭代器,而不是实现容器...
Am I trying at something that isn't a problem? Should I just typedef std::vector< T >::iterator? I am hoping on just depending on the iterator, not the implementing container...
推荐答案
它完全解决了您发布的问题:关于C ++中面向对象和通用编程之间的紧张以及什么类型擦除可以做它
You may find the following article interesting as it addresses exactly the problem you have posted: On the Tension Between Object-Oriented and Generic Programming in C++ and What Type Erasure Can Do About It
这篇关于如何暴露迭代器,而不暴露使用的容器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!