2004-10-28 14:35:53 +00:00
|
|
|
These are some rules for effective C++ programming. These are taken from
|
|
|
|
Scott Meyers, and are presented in their short form. These are not all the
|
|
|
|
rules Meyers presents, only the most important of them. LyX does not yet
|
1999-09-27 18:44:28 +00:00
|
|
|
follow these rules, but they should be the goal.
|
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- use const and inline instead of #define
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- use the same form in corresponding calls to new and delete,
|
1999-09-27 18:44:28 +00:00
|
|
|
i.e. write delete[] obj; if new obj[n]; was used to create
|
|
|
|
the object and write delete obj; if you wrote new obj;
|
2000-08-05 05:17:18 +00:00
|
|
|
Notice strings should be std::string's instead of char *'s.
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- define a default constructor, copy constructor and an assignment
|
2001-02-09 12:29:51 +00:00
|
|
|
operator for all classes with dynamically allocated memory that
|
2008-05-23 19:39:56 +00:00
|
|
|
are not made noncopyable
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- do not define default constructor, copy constructor and an assignment
|
|
|
|
operator if the compiler generated one would do the same
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- make destructors virtual in base classes and only there
|
|
|
|
|
|
|
|
- assign to all data members in operator=()
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
- strive for class interfaces that are complete and minimal
|
|
|
|
|
|
|
|
- differentiate among member functions, global functions and friend
|
2008-05-23 19:39:56 +00:00
|
|
|
functions
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- avoid data members in the public interface
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
- use const whenever possible
|
|
|
|
|
|
|
|
- pass and return objects by reference instead of by value
|
|
|
|
|
|
|
|
- choose carefully between function overloading and
|
2008-05-23 19:39:56 +00:00
|
|
|
parameter defaulting
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
- never return a reference to a local object or a dereferenced
|
2008-05-23 19:39:56 +00:00
|
|
|
pointer initialized by new within the function
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- use enums for integral constants
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- minimize compilation dependencies between files
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
- pay attention to compiler warnings
|
|
|
|
|
|
|
|
- differentiate between inheritance of interface and
|
2008-05-23 19:39:56 +00:00
|
|
|
inheritance of implementation
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
- differentiate between inheritance and templates
|
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- ensure that global objects are initialized before they are used
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2001-12-03 17:41:26 +00:00
|
|
|
- avoid conditions to 'if' and 'while' that span more than a line
|
|
|
|
|
1999-09-27 18:44:28 +00:00
|
|
|
--------
|
|
|
|
|
2004-10-28 14:35:53 +00:00
|
|
|
S. Meyers. Effective C++, 50 Specific Ways to Improve Your Programs and
|
1999-09-27 18:44:28 +00:00
|
|
|
Design. Addison-Wesley, 1992
|
|
|
|
|
|
|
|
==================================
|
|
|
|
|
2003-12-01 13:53:25 +00:00
|
|
|
And one of mine: (Lgb)
|
1999-09-27 18:44:28 +00:00
|
|
|
|
2008-05-23 19:39:56 +00:00
|
|
|
- when swiching on enums, refrain from using "default:" if possible
|
1999-09-27 18:44:28 +00:00
|
|
|
|
|
|
|
|
2003-12-01 13:53:25 +00:00
|
|
|
And one of mine: (Andre')
|
|
|
|
|
|
|
|
- try to implement your class in a way that the automatically generated
|
2008-05-23 19:39:56 +00:00
|
|
|
copy constructor and copy assignment work out-of-the box
|
2003-12-01 13:53:25 +00:00
|
|
|
|