adding the underscores
This commit is contained in:
@@ -95,16 +95,14 @@ translates into:
|
|||||||
```cpp
|
```cpp
|
||||||
v.operator[](i) = 5;
|
v.operator[](i) = 5;
|
||||||
```
|
```
|
||||||
<EFBFBD> In most classes there are two versions of operator[]:
|
In most classes there are two versions of operator[]:
|
||||||
– A non-const version returns a reference to m data[i]. This is applied to non-const Vec objects.
|
– A non-const version returns a reference to m_data[i]. This is applied to non-const Vec objects.
|
||||||
– A const version is the one called for const Vec objects. This also returns m data[i], but as a const
|
– A const version is the one called for const Vec objects. This also returns m_data[i], but as a const reference, so it can not be modified.
|
||||||
reference, so it can not be modified.
|
|
||||||
|
|
||||||
## 8.7 Default Versions of Assignment Operator and Copy Constructor Are Dangerous!
|
## 8.7 Default Versions of Assignment Operator and Copy Constructor Are Dangerous!
|
||||||
|
|
||||||
Before we write the copy constructor and the assignment operator, we consider what would happen if we didn’t
|
Before we write the copy constructor and the assignment operator, we consider what would happen if we didn’t write them.
|
||||||
write them.
|
- C++ compilers provide default versions of these if they are not provided. These defaults just copy the values
|
||||||
<EFBFBD> C++ compilers provide default versions of these if they are not provided. These defaults just copy the values
|
|
||||||
of the member variables, one-by-one. For example, the default copy constructor would look like this:
|
of the member variables, one-by-one. For example, the default copy constructor would look like this:
|
||||||
```cpp
|
```cpp
|
||||||
template <class T>
|
template <class T>
|
||||||
@@ -138,9 +136,9 @@ of the memory of both u and v.
|
|||||||
For Vec (and other classes with dynamically-allocated memory) to work correctly, each object must do its own
|
For Vec (and other classes with dynamically-allocated memory) to work correctly, each object must do its own
|
||||||
dynamic memory allocation and deallocation. We must be careful to keep the memory of each object instance
|
dynamic memory allocation and deallocation. We must be careful to keep the memory of each object instance
|
||||||
separate from all others.
|
separate from all others.
|
||||||
<EFBFBD> All dynamically-allocated memory for an object should be released when the object is finished with it or when
|
- All dynamically-allocated memory for an object should be released when the object is finished with it or when
|
||||||
the object itself goes out of scope (through what’s called a destructor).
|
the object itself goes out of scope (through what’s called a destructor).
|
||||||
<EFBFBD> To prevent the creation and use of default versions of these operations, we must write our own:
|
- To prevent the creation and use of default versions of these operations, we must write our own:
|
||||||
– Copy constructor
|
– Copy constructor
|
||||||
– Assignment operator
|
– Assignment operator
|
||||||
– Destructor
|
– Destructor
|
||||||
@@ -182,23 +180,19 @@ constructor and the assignment operator.
|
|||||||
|
|
||||||
The destructor is called implicitly when an automatically-allocated object goes out of scope or a dynamically allocated object is deleted. It can never be called explicitly!
|
The destructor is called implicitly when an automatically-allocated object goes out of scope or a dynamically allocated object is deleted. It can never be called explicitly!
|
||||||
- The destructor is responsible for deleting the dynamic memory “owned” by the class.
|
- The destructor is responsible for deleting the dynamic memory “owned” by the class.
|
||||||
- The syntax of the function definition is a bit weird. The ~ has been used as a bit-wise inverse or logic negation
|
- The syntax of the function definition is a bit weird. The ~ has been used as a bit-wise inverse or logic negation in other contexts.
|
||||||
in other contexts.
|
|
||||||
|
|
||||||
## 8.14 Increasing the Size of the Vec
|
## 8.14 Increasing the Size of the Vec
|
||||||
|
|
||||||
- push_back(T const& x) adds to the end of the array, increasing m size by one T location. But what if the
|
- push_back(T const& x) adds to the end of the array, increasing m_size by one T location. But what if the allocated array is full (m_size == m_alloc)?
|
||||||
allocated array is full (m size == m alloc)?
|
|
||||||
1. Allocate a new, larger array. The best strategy is generally to double the size of the current array. Why?
|
1. Allocate a new, larger array. The best strategy is generally to double the size of the current array. Why?
|
||||||
2. If the array size was originally 0, doubling does nothing. We must be sure that the resulting size is at
|
2. If the array size was originally 0, doubling does nothing. We must be sure that the resulting size is at least 1.
|
||||||
least 1.
|
|
||||||
3. Then we need to copy the contents of the current array.
|
3. Then we need to copy the contents of the current array.
|
||||||
4. Finally, we must delete current array, make the m data pointer point to the start of the new array, and
|
4. Finally, we must delete current array, make the m_data pointer point to the start of the new array, and adjust the m_size and m_alloc variables appropriately.
|
||||||
adjust the m size and m alloc variables appropriately.
|
|
||||||
- Only when we are sure there is enough room in the array should we actually add the new object to the back
|
- Only when we are sure there is enough room in the array should we actually add the new object to the back of the array.
|
||||||
of the array
|
|
||||||
|
|
||||||
## 8.15 Exercises
|
## 8.15 Exercises
|
||||||
|
|
||||||
Finish the definition of Vec::push_back.
|
- Finish the definition of Vec::push_back.
|
||||||
- Write the Vec::resize function.
|
- Write the Vec::resize function.
|
||||||
|
|||||||
Reference in New Issue
Block a user