Resizing homemade Stack

I have this project in my Data Structures course to create a dynamic stack (otherwise I would use STL in all other cases). I'm implementing the resize feature by making use of the assignment operator and copy constructor, multiplying the capacity by two.

I believe I have implemented the copy constructor and assignment operator properly, using the copy and swap idiom here,

http://stackoverflow.com/questions/3279543/what-is-the-copy-and-swap-idiom

Something is going wrong somewhere though, because when I push a second value onto the stack it never actually gets put into the array.

I've given my best shot so far, any help would be appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
template< typename T >
class MyStack
{

public:

    enum
    {
        MYSTACK_RESIZE_MULTIPLIER   = 2
    };

    /**
     * Constructors and Destructor
     */

    MyStack()
        :   _size(0),
            _capacity(1),
            _stackPtr(new T[1])
    {} //end default constructor

    MyStack(const MyStack< T > &otherStack)
        :   _size(otherStack._size),
            _capacity(otherStack._capacity),
            _stackPtr(new T[otherStack._capacity])
    {
        std::copy(otherStack._stackPtr, otherStack._stackPtr + _size, _stackPtr);

        int i = 0;
    } //end copy constructor

    ~MyStack()
    {
        delete[] _stackPtr;

        _stackPtr = 0;
    } //end desctructor

    /**
     * Operators
     */

    MyStack< T >& operator = (MyStack< T > otherStack)
    {
        return otherStack.swap(*this);
    } //end operator =

    /**
     * Methods
     */

    void push(const T &val)
    {
        if (++_size > _capacity)
            resize();

        _stackPtr[_size - 1] = val;

        int i = 0;
    } //end push()

    MyStack< T >& swap(MyStack< T > &otherStack)
    {
        using std::swap;

        swap(_size, otherStack._size);
        swap(_capacity, otherStack._capacity);
        swap(_stackPtr, otherStack._stackPtr);

        return *this;
    } //end swap()

private:

    void resize()
    {
        _capacity *= MYSTACK_RESIZE_MULTIPLIER;

        MyStack< T > newStack(*this);

        *this = newStack;

        int i = 0;
        
        //newStack will delete automatically when function scope ends
    } //end resize()

private:

    T*  _stackPtr;
    int _size;
    int _capacity;

}; //end MyStack 
PS: the int i = 0 in several of the functions are there just so I can have something to put a break point on and see the result of the previous line in the debugger. Just ignore them.
Never mind, I figured out the problem. The VS debugger does not show all the items in the array, so I didn't think that they were actually there!
The semantics of your operator= look wrong to me. I would expect stack1 == stack2 after using the assignment operator. Not for stack2 to swap contents with stack1.

std::auto_ptr<>::operator= does swap, or move, rather than copy. But most people would steer clear of it these days and use boost::shared_ptr, which supports copy semantics.

Andy

Well it's making a copy of stack2 and then assigning it to stack one. That link in my post proved really useful as a guide on this, but I'm not some of the stuff I'm not exactly sure about.
Topic archived. No new replies allowed.