c++基础笔记3-const修饰返回值

95 阅读1分钟

const 修饰返回值

如果有必要,尽量使用const修饰返回值

#include <iostream>
using namespace std;

const int sum(int a, int b) { return a + b; }

int main() { return 0; }

有什么好处?

如果你不小心把==写成了=,下面的代码会报错。当然也有肯定是好处多余坏处

#include <iostream>
using namespace std;

const int sum(int a, int b) { return a + b; }

int main() {
  if (sum(1, 2) = 3) {
    printf("hello world!");
  }
}