Can someone explain this code?

This is a discussion on Can someone explain this code? within the C++ Programming forums, part of the General Programming Boards category; Flicking through the O'Reilly C++ Quick Reference I saw a snippet of code that I didn't understand at all because ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Can someone explain this code?

    Flicking through the O'Reilly C++ Quick Reference I saw a snippet of code that I didn't understand at all because I've never seen anything like it before:

    Code:
    for SpectrumColor operator++(SpectrumColor &s, int dummy)
    {
        return s = (s >= Violet) ? Red : SpectrumColor(s + 1);
    }
    SpectrumColor is an enumeration as you've probably guessed. I just don't understand this form of the for loop because I've never seen it.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,399
    It looks like a typographical error: without the for, it would just be a non-member postfix operator++.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Also, it seems that it is operator++ for an enum:

    Code:
    #include <iostream>
    enum SpectrumColor { Red, Orange, Yellow, Green, Blue, Indigo, Violet };
    
    SpectrumColor operator++(SpectrumColor &s, int dummy)
    {
        return s = (s >= Violet) ? Red : SpectrumColor(s + 1);
    }
    
    int main()
    {
        SpectrumColor col = Blue;
        col++;
        std::cout << col << ' ';
        col++;
        std::cout << col << '\n';
        col++;
        std::cout << col << '\n';
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Aha, I'd hoped it was an error because it sure as hell didn't look like anything that made sense to me with that "for." Here I was worrying that I'd skipped a couple of chapters.

    Well, I guess I'll check the website for the errata.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing Code
    By ILoveVectors in forum C++ Programming
    Replies: 4
    Last Post: 06-13-2005, 12:27 AM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 03:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 12:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-15-2002, 11:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21