C++——代码习惯

1.主动管理class的拷贝构造函数和赋值构造函数,禁止无参构造函数。

#define DISALLOW_COPY_AND_ASSIGN(classname) \//禁止class的拷贝构造函数和赋值构造函数
 private:                                   \
  classname(const classname &);             \
  classname &operator=(const classname &);

#define DISALLOW_IMPLICIT_CONSTRUCTORS(classname) \//禁止无参构造函数
 private:                                         \
  classname();                                    \
  DISALLOW_COPY_AND_ASSIGN(classname);

 

 

2.语句中用size()就不要用int了,或者用unsinged,否则可能混用。尤其是要判断大小于0的时候,可能出错。

3.for 访问vector

vector<int> v{1,2,3,4,5};
for (auto &i:v)
    i*=i;
for (auto i:v)
    cout<<i<<"";
cout<<endl;

4.变量声明和传递时,考虑是否可读写(const),用引用、指针还是值传递

5.不推荐,能不使用就不使用,尽量不要使用,最好不用#define,而用static const type variable_name=**代替

https://blog.csdn.net/allen807733144/article/details/78667466

 

1.常用习惯

1.单纯常量:使用const、enums代替#define常量,使用static const 作为类内常量

2.宏函数:使用template<T>  inline函数代替#define宏函数

3.函数返回值,能用const尽量用

4.assert代替空{}

5.前缀自增自减

6.cpplint.py检查格式

7.内联函数、枚举、常量代替宏

 

posted @ 2020-03-20 14:30  寒江小筑  阅读(212)  评论(0编辑  收藏  举报