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

猜你喜欢

转载自www.cnblogs.com/yrm1160029237/p/10088213.html