当我在C++中将一个类放在一起时,我几乎总是会做一些事情.
1)虚拟析构函数2)复制构造函数和赋值运算符(我根据名为Copy()的私有函数实现它们,或者将它们声明为私有,从而明确禁止编译器自动生成它们).
你发现什么东西几乎总是有用的?
Jam*_*ran 14
奇怪的是,这里的大多数建议都是我特别不做的事情.
Gre*_*ill 10
我发现打开gcc标志-Wall,-Werror和(这是有趣的)-Weffc++帮助抓住了很多潜在的问题.从gcc手册页:
Run Code Online (Sandbox Code Playgroud)-Weffc++ (C++ only) Warn about violations of the following style guidelines from Scott Meyers’ Effective C++ book: · Item 11: Define a copy constructor and an assignment operator for classes with dynamically allocated memory. · Item 12: Prefer initialization to assignment in constructors. · Item 14: Make destructors virtual in base classes. · Item 15: Have "operator=" return a reference to *this. · Item 23: Don’t try to return a reference when you must return an object. and about violations of the following style guidelines from Scott Meyers’ More Effective C++ book: · Item 6: Distinguish between prefix and postfix forms of incre- ment and decrement operators. · Item 7: Never overload "&&", "??", or ",". If you use this option, you should be aware that the standard library headers do not obey all of these guidelines; you can use grep -v to filter out those warnings.
| 归档时间: |
|
| 查看次数: |
736 次 |
| 最近记录: |