简体   繁体   English

如何在Qt中编写自己的收藏集?

[英]How to write my own collection in qt?

I'm writing some class which will act like a usual collection in QT: 我正在编写一些类,它的作用类似于QT中的常规集合:

class MyCollection
{
  MyElement & at(int) const;
  MyElement & operator[](int) const;
  int count() const;
};

Now I want to enumerate all elements in my new class with QT's foreach. 现在,我想使用QT的foreach枚举新类中的所有元素。 What should I do to achieve this? 我应该怎么做才能做到这一点?

Qt (4.8 at least) uses (from foreach macro) a template wrapper class QForeachContainer (see qglobal.h ) to iterate over smth . Qt(至少为4.8)(从foreach宏中)使用模板包装器类QForeachContainer (请参阅qglobal.h )对smth进行迭代。 Default implementation calls begin() / end() , but you can write your own specialization as well... or just implement iterator begin() , iterator end() members 默认实现调用begin() / end() ,但是您也可以编写自己的专业化...或者只实现iterator begin()iterator end()成员

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM