Saturday, February 12, 2022

[c++] Overloading increment and decrement operators (C++ only)

 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

n8n index

 【n8n免費本地端部署】Windows版|程式安裝x指令大補帖  【一鍵安裝 n8n】圖文教學,獲得無限額度自動化工具&限時免費升級企業版功能