mirror of
https://github.com/cpp-best-practices/cppbestpractices.git
synced 2025-12-17 03:04:36 +03:00
@@ -56,11 +56,23 @@ delete myobj;
|
||||
|
||||
|
||||
// Good Idea
|
||||
std::shared_ptr<MyClass> myobj = make_shared<MyClass>();
|
||||
auto myobj = std::make_unique<MyClass>(); // C++14
|
||||
auto myobj = std::unique_ptr<MyClass>(new MyClass()); // C++11
|
||||
|
||||
// or for reference counted objects
|
||||
|
||||
auto myobj = std::make_shared<MyClass>();
|
||||
|
||||
// ...
|
||||
// myobj is automatically freed for you whenever it is no longer used.
|
||||
```
|
||||
|
||||
## Use `std::array` or `std::vector` Instead of C-style Arrays
|
||||
|
||||
Both of these guarantee contiguous memory layout of objects and can (and should) completely replace your usage of C-style arrays for many of the reasons listed for not using bare pointers.
|
||||
|
||||
Also, [avoid](http://stackoverflow.com/questions/3266443/can-you-use-a-shared-ptr-for-raii-of-c-style-arrays) using `std::shared_ptr` to hold an array.
|
||||
|
||||
## Use Exceptions
|
||||
|
||||
Exceptions cannot be ignored. Return values, such as using `boost::optional`, can be ignored and if not checked can cause crashes or memory errors. An exception, on the other hand, can be caught and handled. Potentially all the way up the highest level of the application with a log and automatic restart of the application.
|
||||
|
||||
Reference in New Issue
Block a user