adding the underscores

This commit is contained in:
Jidong Xiao
2023-09-26 00:07:53 -04:00
parent 53c1d4cabd
commit 820ab5d52f

View File

@@ -95,16 +95,14 @@ translates into:
```cpp
v.operator[](i) = 5;
```
<EFBFBD> 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 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.
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 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.
## 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 didnt
write them.
<EFBFBD> C++ compilers provide default versions of these if they are not provided. These defaults just copy the values
Before we write the copy constructor and the assignment operator, we consider what would happen if we didnt write them.
- 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:
```cpp
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
dynamic memory allocation and deallocation. We must be careful to keep the memory of each object instance
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 whats 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
Assignment operator
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 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
in other contexts.
- 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.
## 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
allocated array is full (m size == m alloc)?
- 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)?
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
least 1.
2. If the array size was originally 0, doubling does nothing. We must be sure that the resulting size is at least 1.
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
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
of the array
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.
- 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.
## 8.15 Exercises
Finish the definition of Vec::push_back.
- Finish the definition of Vec::push_back.
- Write the Vec::resize function.