In the following example, the increment operator is overloaded in both ways:
=========================================================
class X {
public:
// member postfix x++
void operator++(int) { };
};
class Y { };
// nonmember postfix y++
void operator++(Y&, int) { };
int main() {
X x;
Y y;
// calls x.operator++(0)
// default argument of zero is supplied by compiler
x++;
// explicit call to member postfix x++
x.operator++(0);
// calls operator++(y, 0)
y++;
// explicit call to non-member postfix y++
operator++(y, 0);
}
No comments:
Post a Comment