Stack implementation using arrays Please comment on the same. How can it be improved?

Specific improvements I am looking for:


Memory leaks
C++ style
Making code run faster
RAII




#include&lt;iostream&gt;
#include&lt;algorithm&gt;

template &lt;typename T&gt;
class Stack {
private:
    T* array_;
    int length_;
    T* last_;
    void expandArray();

public:
    Stack(int length = 8) :
        array_(new T[length]),
        length_(length),
        last_(array_)
    {}

    Stack&lt;T&gt;&amp; push(const T&amp;);
    Stack&lt;T&gt;&amp; pop();
};

template&lt;typename T&gt;
void Stack&lt;T&gt;::expandArray() {
    T* array_temp = new T[length_ &lt;&lt; 1];
    std::copy(array_, array_ + length_, array_temp);
    std::swap(array_, array_temp);
    delete[] array_temp;
    last_ = array_ + length_ - 1;
    length_ &lt;&lt;= 1;
}

template&lt;typename T&gt;
Stack&lt;T&gt;&amp; Stack&lt;T&gt;::push(const T&amp; data) {
    if (last_ == (array_ + length_ - 1)) {
        expandArray();
    }
    last_[0] = data;
    last_++;
    std::cout &lt;&lt; "[" &lt;&lt; data &lt;&lt; "] pushed." &lt;&lt; std::endl;
    return *this;
}

template&lt;typename T&gt;
Stack&lt;T&gt;&amp; Stack&lt;T&gt;::pop() {
    if(array_ != last_) {
        last_--;
        std::cout &lt;&lt; "[" &lt;&lt; last_[0] &lt;&lt; "] popped." &lt;&lt; std::endl;
    } else {
        std::cout &lt;&lt; "Nothing to pop." &lt;&lt; std::endl;
    }
    return *this;
}

int main() {
    Stack&lt;std::string&gt; s;
    s.push(std::string("a"))
     .push(std::string("b"))
     .push(std::string("c"))
     .push(std::string("d"));
    s.pop().pop().pop().pop().pop();
}
First of all, I would specify that your stack is using a "dynamically allocated array", as opposed to just an array, as I at first expected a C array when looking at the title.  That varies from person to person, though.

On to more important things:


I would not print anything in the stack functions.  If someone wants information on what they're pushing and popping, let them do it themselves.
You seem to be missing a peek function.
Either throw an exception or add an assert to pop for the array_ == last_ case.
length_ should actually be called capacity_.
expandArray may leak memory if the copy throws.  I would use an std::vector&lt;T*&gt; internally for this stuff.
You don't check that length &gt; 0 in the constructor.
I would not allocate so anything by default.
You lack all three of a copy constructor, assignment operator, and destructor, so you'll definitely leak memory.  You should also have a move constructor and a move assignment operator, but those aren't as critical.
You provide no way to check whether the stack is empty.