From 817e492ef082b01dc84c22d66f31abdac0976da7 Mon Sep 17 00:00:00 2001 From: Eric Teunis de Boone Date: Wed, 1 Jul 2020 19:18:47 +0200 Subject: [PATCH] Ex6.3 feedback: Remove Dynamic Allocation in Stack --- ex6.3/Array.hh | 1 + ex6.3/Stack.hh | 22 +++++++++++----------- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/ex6.3/Array.hh b/ex6.3/Array.hh index 84bab41..96e4f79 100644 --- a/ex6.3/Array.hh +++ b/ex6.3/Array.hh @@ -11,6 +11,7 @@ public: Array(const Array& other) : _size(other._size) { _arr = new T[other._size] ; + _default = other._default ; // Copy elements for (int i=0 ; i<_size ; i++) { diff --git a/ex6.3/Stack.hh b/ex6.3/Stack.hh index 4e68fbc..7487532 100644 --- a/ex6.3/Stack.hh +++ b/ex6.3/Stack.hh @@ -8,23 +8,23 @@ template class Stack { // Interface public: - Stack( int in_size ) { - count = 0; - _s = new Array(in_size, 0); - } + Stack( int size ): + _s(size, 0) + {} int nitems() { return count ; } - bool full() { return (count == _s->size()) ; } + bool full() { return (count == _s.size()) ; } bool empty() { return (count == 0) ; } void push(T c) { if (full()) { std::cout << "Stack::push() Error: stack is full" << std::endl ; - _s->resize( _s->size() + 10 ); + _s.resize( _s.size() + 10 ); + } - _s[0][count++] = c ; + _s[count++] = c ; } T pop() { @@ -33,20 +33,20 @@ class Stack { return 0 ; } - return _s[0][--count] ; + return _s[--count] ; } void inspect() { for ( int i = nitems() - 1; i >= 0; i-- ) { - std::cout << i << ": " << _s[0][i] << std::endl; + std::cout << i << ": " << _s[i] << std::endl; } } // Implementation private: - Array* _s ; - int count ; + Array _s ; + int count = 0; }; #endif