天天看點

C++重載函數和重載運算符

重載函數

待寫

重載運算符

c++中的重載運算符非常好用。

比如我們在控制系統中,需要對資料進行濾波,如果用重載運算符,就可以用非常簡潔的重載運算符号完成我們濾波函數的功能。

簡單來說就是用重載運算符取代替函數名。

比如我這裡的這個函數用于處理資料,就是每個資料進來以後,把原資料加3再輸出。

class Lowpassfilter{
  public:
    int operator()(int x);
};

int Lowpassfilter::operator()(int x){
  return x+3;
}

Lowpassfilter low;

void setup(){
  Serial.begin(115200);
}
int i=0;
void loop(){
  Serial.println(low(i));
  i++;
  delay(1000);
}