How to make the for each loop function in C++ work with a custom class

前端 未结 5 1232
遇见更好的自我
遇见更好的自我 2020-12-05 04:18

I\'m new to C/C++ programming, but I\'ve been programming in C# for 1.5 years now. I like C# and I like the List class, so I thought about making a List class in C++ as an e

5条回答
  •  春和景丽
    2020-12-05 04:51

    As @yngum suggests, you can get the VC++ for each extension to work with any arbitrary collection type by defining begin() and end() methods on the collection to return a custom iterator. Your iterator in turn has to implement the necessary interface (dereference operator, increment operator, etc). I've done this to wrap all of the MFC collection classes for legacy code. It's a bit of work, but can be done.

提交回复
热议问题