Comparison tricks in C++

前端 未结 10 1851
礼貌的吻别
礼貌的吻别 2021-01-31 16:13

A class:

class foo{
public:
    int data;
};

Now I want to add a method to this class, to do some comparison, to see if its data is equal to on

10条回答
  •  闹比i
    闹比i (楼主)
    2021-01-31 16:47

    If data is really an integral or enumerated type, you can use a switch:

    switch (data) {
      case 1:
      case 2:
      case 2000:
      case 6000:
      case /* whatever other values you want */:
        act_on_the_group();
        break;
      default:
        act_on_not_the_group();
        break;
    }
    

提交回复
热议问题