C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-18-2009, 10:36 AM   #1
Registered User
 
Sharke's Avatar
 
Join Date: Jun 2008
Location: NYC
Posts: 262
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.
Sharke is offline   Reply With Quote
Old 06-18-2009, 10:42 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 11,353
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 06-18-2009, 11:22 AM   #3
The larch
 
Join Date: May 2006
Posts: 3,222
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.

Quote:
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).
anon is offline   Reply With Quote
Old 06-18-2009, 12:12 PM   #4
Registered User
 
Sharke's Avatar
 
Join Date: Jun 2008
Location: NYC
Posts: 262
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.
Sharke is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing Code ILoveVectors C++ Programming 4 06-13-2005 12:27 AM
True ASM vs. Fake ASM ???? DavidP A Brief History of Cprogramming.com 7 04-02-2003 04:28 AM
Seems like correct code, but results are not right... OmniMirror C Programming 4 02-13-2003 01:33 PM
Interface Question smog890 C Programming 11 06-03-2002 05:06 PM
Can you have nested code block? What does the compiler do? For example ... albertr C Programming 4 01-16-2002 12:04 AM


All times are GMT -6. The time now is 06:52 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

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