C++ const keyword - use liberally?

前端 未结 12 2418
甜味超标
甜味超标 2020-12-04 18:12

In the following C++ functions:

void MyFunction(int age, House &purchased_house)
{
    ...
}


void MyFunction(const int age, House &purchased_house)         


        
12条回答
  •  爱一瞬间的悲伤
    2020-12-04 18:49

    One benefit of using const is that you cannot accidentally change the value of age in the middle of MyFunction (in the event you forgot it was not pass by reference). One "disadvantage" is that you can't recycle age with code like foo.process(++age);.

提交回复
热议问题