diff options
Diffstat (limited to 'Homework8/MyStructures/ContactList.hpp')
| -rw-r--r-- | Homework8/MyStructures/ContactList.hpp | 119 |
1 files changed, 119 insertions, 0 deletions
diff --git a/Homework8/MyStructures/ContactList.hpp b/Homework8/MyStructures/ContactList.hpp new file mode 100644 index 0000000..dd0f4ee --- /dev/null +++ b/Homework8/MyStructures/ContactList.hpp @@ -0,0 +1,119 @@ +#ifndef CONTACT_LIST_HPP +#define CONTACT_LIST_HPP + +namespace MyStructures +{ + template<class C> + class ContactList + { + public: + ContactList() = default; + ContactList(size_t size); //Creates contact list of this size + ContactList(const C* contacts, const size_t& length); //Makes a copy of another contact list + ~ContactList(); + + ContactList(const ContactList& copy); //deep copy constructor + ContactList& operator=(const ContactList& rhs); //deep copy assignment + + ContactList(ContactList&& move); //reference(move) constructor + ContactList& operator=(ContactList&& rhs); //reference(move) assignment + + private: + C* contacts_{ nullptr }; + size_t length_{ 0 }; //entire length of array + size_t size_{ 0 }; //number of actual elements + + C* AllocateContactList(const size_t& length); + }; + template<class C> + ContactList<C>::ContactList(size_t size) + { + contacts_ = AllocateContactList(size); + } + template<class C> + ContactList<C>::ContactList(const C* contacts, const size_t& length) + { + contacts_ AllocateContactList(length); + for (auto i = 0u; i < length; ++i) + { + contacts_[i] = contacts[i]; + } + } + template<class C> + ContactList<C>::~ContactList() + { + delete[] contacts_; + contacts_ = nullptr; + } + template<class C> + ContactList<C>::ContactList(const ContactList& copy) + { + length_ = copy.length_; + size_ = copy.size_; + contacts_ = AllocateContactList(copy.length_); + + contacts_ AllocateContactList(length); + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = copy[i]; + } + + } + template<class C> + ContactList<C>&ContactList<C>::operator=(const ContactList& rhs) + { + if (this != &rhs) //asking if they're the same object + { + delete[] contacts_; + contacts_ = nullptr; + + contacts_ = AllocateContactList(rhs.length_); + size_ = rhs.size_; + contacts_ AllocateContactList(length); + + for (auto i = 0u; i < length_; ++i) + { + contacts_[i] = rhs[i]; + } + } + return *this; + } + template<class C> + ContactList<C>::ContactList(ContactList&& move) + { + *this = std::move(move); //move shifts addresses + } + template<class C> + ContactList<C>& ContactList<C>::operator=(ContactList&& rhs) + { + if(this != &rhs) + { + delete[] contacts_; + contacts = nullptr; + + contacts_ = rhs.contacts_; + + length_ = rhs.length_; + size_ = rhs.size_; + + rhs.contacts_ = nullptr; //Invalidate the other object + } + template<class C> + C* ContactList<C>::AllocateContactList(const size_t& length) + { + C* storage = nullptr + length_ - length; + + storage = newC[length]{}; + + return storage; + } +}; +} + + + + + + +#endif
\ No newline at end of file |