Results 1 to 12 of 12

Thread: C++ and C#

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 14 Times in 12 Posts

    Default Re: C++ and C#

    Actually it should be possible to emulate the whole IEnumerable interface with a class wrapping around a simple list structure.
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: C++ and C#

    Isn't it what iterators do?

  3. #3
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 14 Times in 12 Posts

    Default Re: C++ and C#

    Quote Originally Posted by wysota View Post
    Isn't it what iterators do?
    Yes, it is almost exactly the same. The difference simply is that the yield keyword and the IEnumarable interface hide the whole iterator concept, and as such adds nothing but some syntactic sugar (to some, at least).
    "If you lie to the compiler, it will get its revenge." - Henry Spencer

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: C++ and C#

    Consider the following example:
    Qt Code:
    1. SomeContainerObject Elements;
    2. MyIterator iter(Elements);
    3. while(iter.hasNext()){
    4. Element e = iter.next();
    5. doSomething(e);
    6. }
    To copy to clipboard, switch view to plain text mode 
    Isn't this exactly what is searched for? And this is exactly a typical iterator. Of course hasNext() and next() have to be implemented in such a way that they perform the same function what the yieldable function does.

    What is vital is that the iterator keeps an internal state of "index" in the iterated object. Then it's just a matter of generating elements. Of course the object doesn't even have to be a container - in that case the iterator becomes a simple generator function, like this:
    Qt Code:
    1. class Generator {
    2. public:
    3. Generator(int start){
    4. m_curr = start;
    5. }
    6. bool hasNext(){ return true; /* neverending story */ }
    7. int next(){ return -(m_curr++); }
    8. private:
    9. int m_curr;
    10. }
    To copy to clipboard, switch view to plain text mode 
    The above object keeps returning negations of increasing numbers starting from the number passed as the argument for the generator. It's similar to:
    Qt Code:
    1. int fun(int s){
    2. int x = s;
    3. while(1){
    4. int r = -x;
    5. x = x+1;
    6. yield r;
    7. }
    8. }
    To copy to clipboard, switch view to plain text mode 

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.