C++ const用法总结

来源:互联网 发布:淘宝看店宝 编辑:程序博客网 时间:2024/05/22 00:39
 1. 修饰变量, 左结合, 修饰左边相邻的符号
     a) int const i = 12; // 变量是const的
     注意: const T t 和T const t是等价的, 但是建议采用第二种写法, 因为在template里前一种写法可能导致模板在某些情况下不能被实例化.
     b) int * const p; // 指针是const的
  2. 修饰函数参数
     void foo1 (bar const  &);
     void foo2 (bar const *);
  3. 修饰成员函数
     class Foo
     {
            public:
                 void foo()  const
                 {
                        // do some stuff that don't modify the data members of Foo
                 }
     }
原创粉丝点击