Thread: Can someone explain this code?

  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
    28,412
    It looks like a typographical error: without the for, it would just be a non-member postfix operator++.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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, 04: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, 01: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-16-2002, 12:04 AM