Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

 
 
Thread Tools Search this Thread Display Modes
Prev Previous Post   Next Post Next
Old Feb 21, 2013, 08:13 PM   #1
ArtOfWarfare
macrumors 68040
 
ArtOfWarfare's Avatar
 
Join Date: Nov 2007
Send a message via Skype™ to ArtOfWarfare
Any thoughts on the comma operator (C/C++/Obj-C?)

I recently decided to insert a few comma operators into the output lines of my C++ code and I feel like they've cleaned up my code a lot.

For example, I had this code:

Code:
cout << "Can't place " << v << " at " << x << ", " << y << endl << "Add: ";
printBitsForInt(mask);
cout << "Old: ";
printBitsForInt(conflicts[v-1]);
cout << endl;
Just an FYI, printBitsForInt() takes an int and prints out the bits from it. I couldn't find any way to overload the insertion operator between a stream and an int (because it's already defined by C++.) I couldn't find any stream parameters for cout I could set to make it print ints as binary, either. I tried using a bitset container, but it didn't allow its contents to be printed out with the grouping I wanted (groups of nine bits with spaces between them.)

And by introducing a few sequence points I feel like I was able to clean it up quite a bit, improve legibility, and reduce the amount of space it takes up.

Code:
cout << "Can't place " << v << " at " << x << ", " << y << endl
     << "Add: ", printBitsForInt(mask), cout << endl;
     << "Old: ", printBitsForInt(conflicts[v-1]), cout << endl;
I'm a little confused why I so rarely see anyone using them.

I'm wondering if I could use the same basic idea in the future with some C or Obj-C code.

Also, is there any real difference between using the comma operator and inserting a semicolon? I feel like the compiler probably generates the same code for both, wouldn't it?

Are there any other good uses for the comma operator?
__________________
Battery Status - On the Mac App Store
The only app that'll estimate when your wireless devices will need their batteries changed.
Like it on Facebook!
ArtOfWarfare is offline   0 Reply With Quote

 
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 05:09 PM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Powered by vBulletin® Version 3.8.6
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

Privacy / DMCA contact / Affiliate and FTC Disclosure
Copyright 2002-2013, MacRumors.com, LLC