|
Operator comma
The comma
operator is called when it
appears next to an object of the type the comma is defined for. However,
“operator,” is not called for function argument
lists, only for objects that are out in the open, separated by commas. There
doesn’t seem to be a lot of practical uses for this operator; it’s
in the language for consistency. Here’s an example showing how the comma
function can be called when the comma appears before an object, as well
as after:
//: C12:OverloadingOperatorComma.cpp
#include <iostream>
using namespace std;
class After {
public:
const After& operator,(const After&) const {
cout << "After::operator,()" << endl;
return *this;
}
};
class Before {};
Before& operator,(int, Before& b) {
cout << "Before::operator,()" << endl;
return b;
}
int main() {
After a, b;
a, b; // Operator comma called
Before c;
1, c; // Operator comma called
} ///:~
The global function allows the comma to
be placed before the object in question. The usage shown is fairly obscure and
questionable. Although you would probably use a comma-separated list as part of
a more complex expression, it’s too subtle to use in most
situations.
|
|