|
When we think of the comma character we often think of it as a separator. It separates values in an CSV file, it separates items in lists, it may separate index values in multidimensional arrays (in other languages) and more. But in C++ an obscure way of using them was as an operator. The reason it never made it big as an operator and into the typical programming psyche is that largely it is seen as useless or some kind of "syntactic sugar". So in this article we try to show how it works, why it has a stigma and some possible uses for it that may prove useful at some point in your career going forward. Eats(), Shoots() and Leaves();
|
|
|
|
|
Why does he keep saying C++, it's a C thing. And C# doesn't have it.
I think the only times I've use the comma operator is in for loops:
for ( i = 3 , j = 5 ; ... ; ... ) ...
for ( ... ; i = GetSomething() , j < k ; ... ) ...
for ( ... ; ... ; i++ , j++ ) ...
|
|
|
|
|
That's not what they are getting at. In C++ it's an operator proper and can be overloaded.
Steve
modified 14-Feb-13 7:34am.
|
|
|
|
|
In C++, ypu* can overload it, allowing e.g.
EnableDlgItems(hwnd, false), IDC_FOO, IDC_BAR, IDC_BAZ;
*) also, you.
|
|
|
|
|
Kind of misses the best bits sadly. The comma operator can be used to explicitly control order of operation, i.e. prevent the compiler from reordering things that aren't directly related but have a hidden ordering dependency. Using the comma operator this way is the solution to Alexander Andrescu's pure C++ portable mutex implementation problem. I must write this up with a test case at some point, busy, busy, busy
"The secret of happiness is freedom, and the secret of freedom, courage."
Thucydides (B.C. 460-400)
|
|
|
|