如何在不暴露使用的容器的情况下暴露迭代器?

编程入门 行业动态 更新时间:2024-10-27 14:27:56
本文介绍了如何在不暴露使用的容器的情况下暴露迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我使用 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

更多推荐

如何在不暴露使用的容器的情况下暴露迭代器?

本文发布于:2023-11-23 09:15:56,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1620905.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:容器   情况下   迭代   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!