7template<
typename ElementType>
16 if (this->m_size != 0 || m_ptr !=
nullptr)
19 this->m_ptr =
nullptr;
26 this->m_ptr =
nullptr;
31 size_t size()
const {
return this->m_size; }
37 else if(
count == this->m_size)
47 this->m_ptr =
new ElementType[this->m_size];
62 stream <<
"Default constructor called" <<
endl;
66 this->m_ptr =
size > 0 ?
new ElementType[
size] :
nullptr;
78 m_size{right_hand_side.m_size}
84 this->m_ptr = this->m_size > 0 ?
new ElementType[this->m_size] :
nullptr;
87 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
99 ElementType &
operator[](
size_t index) {
return this->m_ptr[index]; }
101 const ElementType &
operator[](
size_t index)
const {
return this->m_ptr[index]; }
103 ElementType &
at(
size_t index)
105 if (index < this->m_size && this->m_ptr !=
nullptr)
106 return this->m_ptr[index];
111 const ElementType &
at(
size_t index)
const
113 if (index < this->m_size && this->m_ptr !=
nullptr)
114 return this->m_ptr[index];
123 stream <<
"Copy assignment operator() called" <<
endl;
130 if (
this != std::addressof(right_hand_side))
132 if (this->m_size == right_hand_side.m_size)
139 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
145 this->m_size = right_hand_side.m_size;
147 if (this->m_size > 0)
152 this->m_ptr =
new ElementType[this->m_size];
153 std::memcpy(this->m_ptr, right_hand_side.m_ptr, m_size *
sizeof(ElementType));
168 : m_size{right_hand_side.m_size},
169 m_ptr{right_hand_side.m_ptr}
176 right_hand_side.invalidate();
181 stream <<
"Move assignment operator() called" <<
endl;
183 if (
this != std::addressof(right_hand_side))
187 this->m_size = right_hand_side.m_size;
188 this->m_ptr = right_hand_side.m_ptr;
193 right_hand_side.invalidate();
215 for(
size_t i = 0; i <
size; ++i)
218 os << da[
size] <<
" }";
229 for(
size_t i = 0; i < a.size(); ++i)
239 std::vector< dynamic_array<int> > jagged_array(
count);
241 stream <<
"At this point, default constructor of dynamic_array is called "
244 for(
size_t i = 0; i < jagged_array.size(); ++i)
247 stream <<
"In the for loop, default constructor of dynamic_array is called "
248 <<
count <<
" times, and move constructor is called " <<
count <<
" times\n" <<
endl;
255 stream <<
"\nthis is better, but not perfect!!\n" <<
endl;
259 std::vector< dynamic_array<int> > jagged_array;
260 jagged_array.reserve(
count);
262 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
269 stream <<
"\nthis is PEREFECT WAY, YOU SHOULD ALWAYS USE THIS METHOD!!\n" <<
endl;
273 std::vector< dynamic_array<int> > jagged_array;
274 jagged_array.reserve(
count);
276 for(
size_t i = 0; i < jagged_array.capacity(); ++i)
278 jagged_array.emplace_back(i + 1);
285 for(
size_t j = 0; j < jagged_array.back().size(); ++j)
286 jagged_array.back()[j] = (int)j;
291 for(
auto& da: jagged_array)
void examples_for_dynamic_array()
void please_use_this_method()
void is_still_is_not_perfect()
ElementType * operator&()
dynamic_array(dynamic_array &&right_hand_side) noexcept
ElementType & operator[](size_t index)
dynamic_array(const dynamic_array &right_hand_side)
const ElementType & at(size_t index) const
void resize(size_t count)
dynamic_array & operator=(dynamic_array &&right_hand_side) noexcept
ElementType & at(size_t index)
const ElementType & operator[](size_t index) const
friend std::ostream & operator<<(std::ostream &os, const dynamic_array &da)
dynamic_array(size_t size=1)
dynamic_array & operator=(const dynamic_array &right_hand_side)
Stream output operators << are implemented.
#define Tpf_ThrowDebugException(debug_message)
Throw a debug_exception with message as argument.