Several minor improvements

Fixed some typos, added some links and some further minor improvements
This commit is contained in:
rob100
2015-08-04 14:57:28 +02:00
parent 7ce6ceac36
commit 83de05d9cd
5 changed files with 13 additions and 14 deletions

View File

@@ -139,8 +139,8 @@ Header files must contain a distinctly-named include guard to avoid problems wit
#define MYPROJECT_MYCLASS_HPP #define MYPROJECT_MYCLASS_HPP
namespace MyProject { namespace MyProject {
class MyClass { class MyClass {
}; };
} }
#endif #endif
@@ -274,7 +274,7 @@ There is almost never a reason to declare an identifier in the global namespaces
## Use the Correct Integer Type For stdlib Features ## Use the Correct Integer Type For stdlib Features
The standard library generally returns `size_t` for anything related to size. What exactly `size_t` is is implementation defined. The standard library generally returns `size_t` for anything related to size. What exactly `size_t` is, is implementation defined.
In general, using `auto` will avoid most of these issues, but not all. In general, using `auto` will avoid most of these issues, but not all.
@@ -312,10 +312,10 @@ However, you can easily create unreadable expressions using too much or wrong op
More detailed, you should keep these things in mind: More detailed, you should keep these things in mind:
* Overloading `operator=` when handling with resources is a must, see "Consider the Rule of Zero" below. * Overloading `operator=` when handling with resources is a must, see [Consider the Rule of Zero](03-Style.md#consider-the-rule-of-zero) below.
* For all other operators, only overload them when they are used in a context that is commonly connected to these operators. Typical scenarios are concatenating things with +, negating expressions that can be considered "true" or "false", etc. * For all other operators, only overload them when they are used in a context that is commonly connected to these operators. Typical scenarios are concatenating things with +, negating expressions that can be considered "true" or "false", etc.
* Always be aware of the [operator precedence](http://en.cppreference.com/w/cpp/language/operator_precedence) and try to circumvent unintuitive constructs. * Always be aware of the [operator precedence](http://en.cppreference.com/w/cpp/language/operator_precedence) and try to circumvent unintuitive constructs.
* Do not overload exotic operators such as ~ or %. * Do not overload exotic operators such as ~ or %.
* [Never](http://stackoverflow.com/questions/5602112/when-to-overload-the-comma-operator?answertab=votes#tab-top) overload `operator ,` (the comma operator). * [Never](http://stackoverflow.com/questions/5602112/when-to-overload-the-comma-operator?answertab=votes#tab-top) overload `operator ,` (the comma operator).
* Use `operator >>` and `operator <<` when dealing with streams. For example, you can overload `operator <<(std::ostream &, MyClass const &)` to enable "writing" you class into a stream, such as std::cout or an std::fstream or std::stringstream. The latter is often used to create a textual representation of a value. * Use `operator >>` and `operator <<` when dealing with streams. For example, you can overload `operator <<(std::ostream &, MyClass const &)` to enable "writing" you class into a stream, such as std::cout or an std::fstream or std::stringstream. The latter is often used to create a textual representation of a value.
* There are more common operators to overload [described here](http://stackoverflow.com/questions/4421706/operator-overloading?answertab=votes#tab-top) * There are more common operators to overload [described here](http://stackoverflow.com/questions/4421706/operator-overloading?answertab=votes#tab-top)
@@ -332,7 +332,7 @@ Instead mark single parameter constructors as `explicit`, which requires them to
### Conversion Operators ### Conversion Operators
Similarly to single parameter constructors, conversion operators can be called by the compiler and introduce unexpected overhead. The should also be marked as `explicit`. Similarly to single parameter constructors, conversion operators can be called by the compiler and introduce unexpected overhead. They should also be marked as `explicit`.
## Consider the Rule of Zero ## Consider the Rule of Zero

View File

@@ -21,7 +21,7 @@ public:
private: private:
std::string m_value; std::string m_value;
} };
// Good Idea // Good Idea
@@ -40,7 +40,7 @@ public:
private: private:
std::string m_value; std::string m_value;
} };
``` ```
### Consider Return By Value for Mutable Data, `const &` for Immutable ### Consider Return By Value for Mutable Data, `const &` for Immutable
@@ -68,7 +68,6 @@ auto mybuffer = std::make_unique<char[]>(length); // C++14
auto mybuffer = std::unique_ptr<char[]>(new char[length]); // C++11 auto mybuffer = std::unique_ptr<char[]>(new char[length]); // C++11
// or for reference counted objects // or for reference counted objects
auto myobj = std::make_shared<MyClass>(); auto myobj = std::make_shared<MyClass>();
// ... // ...
@@ -88,7 +87,7 @@ Exceptions cannot be ignored. Return values, such as using `boost::optional`, ca
Stroustrup, the original designer of C++, [makes this point](http://www.stroustrup.com/bs_faq2.html#exceptions-why) much better than I ever could. Stroustrup, the original designer of C++, [makes this point](http://www.stroustrup.com/bs_faq2.html#exceptions-why) much better than I ever could.
## Use C++-style cast instead of C-style cast ## Use C++-style cast instead of C-style cast
Use the C++-style cast(static\_cast<>, dynamic\_cast<> ...) instead of the C-style cast. The C++-style cast allows more compiler checks and is considerable safer. Use the C++-style cast (static\_cast<>, dynamic\_cast<> ...) instead of the C-style cast. The C++-style cast allows more compiler checks and is considerable safer.
```cpp ```cpp
// Bad Idea // Bad Idea
@@ -98,7 +97,7 @@ int i = (int) x;
// Good Idea // Good Idea
int i = static_cast<int>(x); int i = static_cast<int>(x);
``` ```
Additionaly the C++ cast style is more visible and has the possiblity to search for. Additionally the C++ cast style is more visible and has the possibility to search for.
## Additional Resources ## Additional Resources

View File

@@ -17,7 +17,7 @@ namespace my_project {
// static const double 3.14159 = 3.14159; // static const double 3.14159 = 3.14159;
// which leads to a compile-time error. Sometimes such errors are hard to understand. // which leads to a compile-time error. Sometimes such errors are hard to understand.
static const double PI = 3.14159; static const double PI = 3.14159;
} };
} }
``` ```

View File

@@ -14,7 +14,7 @@ Besides being global data, statics are not always constructed and deconstructed
### Singletons ### Singletons
A singleton is often implemented with a static and/or `shared_ptr` A singleton is often implemented with a static and/or `shared_ptr`.
## Avoid Heap Operations ## Avoid Heap Operations

View File

@@ -8,4 +8,4 @@
* https://svn.boost.org/trac/boost/wiki/BestPracticeHandbook - Best Practice Handbook from Nial Douglas * https://svn.boost.org/trac/boost/wiki/BestPracticeHandbook - Best Practice Handbook from Nial Douglas
* http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=ListOfChecks * http://sourceforge.net/apps/mediawiki/cppcheck/index.php?title=ListOfChecks
* http://emptycrate.com/ * http://emptycrate.com/
* http://stackoverflow.com/questions/tagged/c%2b%2b-faq?sort=votes&pageSize=15 StackOverflow C++ FAQ * http://stackoverflow.com/questions/tagged/c%2b%2b-faq?sort=votes&pageSize=15 - StackOverflow C++ FAQ