How can I expose iterators without exposing the container used?(如何在不暴露使用的容器的情况下暴露迭代器?)
问题描述
我使用 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 >::迭代器?我希望只依赖于迭代器,而不是实现容器......
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
这篇关于如何在不暴露使用的容器的情况下暴露迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在不暴露使用的容器的情况下暴露迭代器?


基础教程推荐
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 我有静态或动态 boost 库吗? 2021-01-01
- 常量变量在标题中不起作用 2021-01-01
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09