C++动态数组operator+重载:实现两种元素添加方式
Great question! The issue here is that your current operator+ is a member function, which means it can only handle cases where the left-hand operand is a DynamicVector instance (like array + element). To support element + array, we need a non-member global operator function since the left operand is a TElement, not a DynamicVector.
First, let's fix a small semantic issue with your existing code: your current member operator+ modifies the original DynamicVector and returns a reference to it. That's actually the behavior we expect from operator+=, not operator+. The standard convention for operator+ is to return a new copy of the object with the modification, leaving the original untouched. Let's adjust that first to follow best practices.
Here's how to implement both cases properly:
Step 1: Refactor to operator+= (for in-place modification)
Keep your existing logic but rename it to operator+=—this is the right place for modifying the original array directly:
template <typename TElement> DynamicVector<TElement>& DynamicVector<TElement>::operator+=(const TElement& e) { if (this->size == this->capacity) this->resize(); this->elems[this->size] = e; this->size++; return *this; }
Step 2: Implement member operator+ for array + element
Now build operator+ on top of operator+= to return a new array without changing the original:
template <typename TElement> DynamicVector<TElement> DynamicVector<TElement>::operator+(const TElement& e) const { DynamicVector<TElement> result = *this; // Create a copy of the original array result += e; // Use our += operator to add the element to the copy return result; }
Step 3: Implement global operator+ for element + array
Since the left operand is a TElement (not a DynamicVector), we can't use a member function. We'll create a global template function, and declare it as a friend inside DynamicVector to access private members like size, capacity, and resize:
First, add this friend declaration inside your DynamicVector class definition:
template <typename T> friend DynamicVector<T> operator+(const T& e, const DynamicVector<T>& vec);
Then implement the global function:
template <typename TElement> DynamicVector<TElement> operator+(const TElement& e, const DynamicVector<TElement>& vec) { DynamicVector<TElement> result; result += e; // Add the standalone element first // Append all elements from the original vector for (int i = 0; i < vec.size; ++i) { result += vec.elems[i]; } return result; }
Bonus: Simplify with a vector-to-vector operator+=
If you want to make the global operator+ cleaner, add an operator+= that takes another DynamicVector:
template <typename TElement> DynamicVector<TElement>& DynamicVector<TElement>::operator+=(const DynamicVector<TElement>& other) { for (int i = 0; i < other.size; ++i) { *this += other.elems[i]; } return *this; }
Then the global operator+ becomes much more concise:
template <typename TElement> DynamicVector<TElement> operator+(const TElement& e, const DynamicVector<TElement>& vec) { DynamicVector<TElement> result; result += e; result += vec; // Use our new vector-to-vector += operator return result; }
Why this works:
array + element: Uses the memberoperator+, which creates a copy ofarray, adds the element to the copy, and returns it (leaving the original array unchanged).element + array: Uses the globaloperator+, which creates a new empty vector, adds the standalone element first, then appends all elements from the original array.
This follows C++ operator overloading best practices: operator+ is non-modifying (returns a new object), while operator+= handles in-place changes.
内容的提问来源于stack exchange,提问作者Doull Owen

