line breaks
This commit is contained in:
@@ -22,6 +22,8 @@ have performance and memory usage disadvantages.
|
|||||||
|
|
||||||
- For our discussion today, we’ll assume that all program data is stored in dynamically-allocated instances of the
|
- For our discussion today, we’ll assume that all program data is stored in dynamically-allocated instances of the
|
||||||
following simple class. This class can be used to build linked lists, trees, and graphs with cycles:
|
following simple class. This class can be used to build linked lists, trees, and graphs with cycles:
|
||||||
|
|
||||||
|
```cpp
|
||||||
class Node {
|
class Node {
|
||||||
public:
|
public:
|
||||||
Node(char v, Node* l, Node* r) :
|
Node(char v, Node* l, Node* r) :
|
||||||
@@ -30,6 +32,7 @@ public:
|
|||||||
Node* left;
|
Node* left;
|
||||||
Node* right;
|
Node* right;
|
||||||
};
|
};
|
||||||
|
```
|
||||||
|
|
||||||
## 25.3 Garbage Collection Technique #1: Reference Counting
|
## 25.3 Garbage Collection Technique #1: Reference Counting
|
||||||
|
|
||||||
@@ -167,11 +170,11 @@ also when we are developing big complex programs that process and rearrange lots
|
|||||||
enormous beast of a programming language (but that doesn’t stop people from trying!). So is there anything
|
enormous beast of a programming language (but that doesn’t stop people from trying!). So is there anything
|
||||||
we can do? Yes, we can use Smart Pointers to gain some of the features of garbage collection.
|
we can do? Yes, we can use Smart Pointers to gain some of the features of garbage collection.
|
||||||
Some examples below are modified from these nice online references:
|
Some examples below are modified from these nice online references:
|
||||||
http://ootips.org/yonat/4dev/smart-pointers.html
|
[http://ootips.org/yonat/4dev/smart-pointers.html](http://ootips.org/yonat/4dev/smart-pointers.html)
|
||||||
http://www.codeproject.com/KB/stl/boostsmartptr.aspx
|
[http://www.codeproject.com/KB/stl/boostsmartptr.aspx](http://www.codeproject.com/KB/stl/boostsmartptr.aspx)
|
||||||
http://en.wikipedia.org/wiki/Smart_pointer
|
[http://en.wikipedia.org/wiki/Smart_pointer](http://en.wikipedia.org/wiki/Smart_pointer)
|
||||||
http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm
|
[http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm](http://www.boost.org/doc/libs/1_48_0/libs/smart_ptr/smart_ptr.htm)
|
||||||
http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/
|
[http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/](http://www.acodersjourney.com/2016/05/top-10-dumb-mistakes-avoid-c-11-smart-pointers/)
|
||||||
|
|
||||||
## 25.12 What’s a Smart Pointer?
|
## 25.12 What’s a Smart Pointer?
|
||||||
|
|
||||||
@@ -184,12 +187,12 @@ obsess about how & when it will get deleted (it happens automatically).
|
|||||||
template <class T>
|
template <class T>
|
||||||
class auto_ptr {
|
class auto_ptr {
|
||||||
public:
|
public:
|
||||||
explicit auto_ptr(T* p = NULL) : ptr(p) {} /* prevents cast/conversion */
|
explicit auto_ptr(T* p = NULL) : ptr(p) {} /* prevents cast/conversion */
|
||||||
~auto_ptr() { delete ptr; }
|
~auto_ptr() { delete ptr; }
|
||||||
T& operator*() { return *ptr; }
|
T& operator*() { return *ptr; }
|
||||||
T* operator->() { return ptr; } /* fakes being a pointer */
|
T* operator->() { return ptr; } /* fakes being a pointer */
|
||||||
private:
|
private:
|
||||||
T* ptr;
|
T* ptr;
|
||||||
};
|
};
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -197,17 +200,17 @@ T* ptr;
|
|||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
void foo() {
|
void foo() {
|
||||||
Polygon* p(new Polygon(/* stuff */));
|
Polygon* p(new Polygon(/* stuff */));
|
||||||
p->DoSomething();
|
p->DoSomething();
|
||||||
delete p;
|
delete p;
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
- Here’s how we can re-write the same example with our auto_ptr:
|
- Here’s how we can re-write the same example with our auto_ptr:
|
||||||
|
|
||||||
```cpp
|
```cpp
|
||||||
void foo() {
|
void foo() {
|
||||||
auto_ptr<Polygon> p(new Polygon(/* stuff */);
|
auto_ptr<Polygon> p(new Polygon(/* stuff */);
|
||||||
p->DoSomething();
|
p->DoSomething();
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
@@ -230,7 +233,7 @@ std::vector<Polygon*> polys;
|
|||||||
polys.push_back(new Triangle(/*...*/));
|
polys.push_back(new Triangle(/*...*/));
|
||||||
polys.push_back(new Quad(/*...*/));
|
polys.push_back(new Quad(/*...*/));
|
||||||
for (unsigned int i = 0; i < polys.size(); i++) {
|
for (unsigned int i = 0; i < polys.size(); i++) {
|
||||||
delete polys[i];
|
delete polys[i];
|
||||||
}
|
}
|
||||||
polys.clear();
|
polys.clear();
|
||||||
```
|
```
|
||||||
|
|||||||
Reference in New Issue
Block a user