• 4400阅读
  • 0回复

C++ basics: use const wherever you can [复制链接]

上一主题 下一主题
离线steinlee
 

只看楼主 倒序阅读 楼主  发表于: 2010-03-07
Try to use const as many as possible. Using const to a variable or a pointer means that the value or address can not be changed.
Using const to a func means that this function does not change any values inside the class.
The advantage of using const is that the compiler will tell you if you make some related mistakes.

Examples:
   void A::func( const B * const b ); //in case b can not be changed in this function.
    first const means only const functions of B can be used here
    second const means the address of b can not be changed
  
    double A::getValue() const { return value; } //no value change inside a class
Looking for remote C/C++ and Qt 兼职
快速回复
限100 字节
 
上一个 下一个