Skip to content

Commit

Permalink
Implement assignment operator for the List class.
Browse files Browse the repository at this point in the history
  • Loading branch information
BartVandewoestyne committed Feb 15, 2024
1 parent 2915aaf commit c84d66d
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Foundation_Classes/List.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,25 @@ List<Item>::~List() {
delete[] _items;
}

// TODO: operator=

// Implements the assignment operation to assign member data properly.
template<class Item>
List<Item>& List<Item>::operator=(const List<Item>& other) {
if (this != &other) { // Check for self-assignment
// Clear the current list
RemoveAll();

// Allocate memory for the new list
_size = other._size;
_count = other._count;
_items = new Item[_size];

// Copy elements from the other list to this list
for (long i = 0; i < _count; ++i) {
_items[i] = other._items[i];
}
}
return *this;
}

template<class Item>
long List<Item>::Count() const {
Expand Down

0 comments on commit c84d66d

Please sign in to comment.