Skip to content

Commit 56b7a48

Browse files
committed
Fix operator[] compiler bug.
Refactor memeber name.
1 parent 7f28fd6 commit 56b7a48

File tree

1 file changed

+35
-38
lines changed

1 file changed

+35
-38
lines changed

Algorithms/Stack.h

+35-38
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ namespace Algorithm {
2626
bool Reallocation();
2727
bool IsCapacityEnough()const;
2828

29-
T* mStack;
30-
int mCount;
31-
int mCapacity;
29+
T* m_stack;
30+
int m_count;
31+
int m_capacity;
3232
};
3333

3434
//-------------------define
@@ -40,9 +40,9 @@ namespace Algorithm {
4040
inline Stack<T>::Stack()
4141
{
4242

43-
mCount = 0;
44-
mCapacity = 0;
45-
mStack = nullptr;
43+
m_count = 0;
44+
m_capacity = 0;
45+
m_stack = nullptr;
4646

4747
Initialize(DEFAULTCapacity);
4848
}
@@ -61,66 +61,63 @@ namespace Algorithm {
6161
Reallocation();
6262
}
6363

64-
mStack[mCount] = value;
64+
m_stack[m_count] = value;
6565

66-
mCount++;
66+
m_count++;
6767

6868
}
6969

7070
template<typename T>
7171
inline T Stack<T>::Pop()
7272
{
73-
if (mCount <= 0)
74-
{
75-
return nullptr;
76-
}
73+
assert(m_count > 0);
7774

78-
T t = mStack[mCount - 1];
75+
T t = m_stack[m_count - 1];
7976

80-
mCount--;
77+
m_count--;
8178

8279
return t;
8380
}
8481

8582
template<typename T>
8683
inline void Stack<T>::Clear()
8784
{
88-
if (mStack != nullptr)
85+
if (m_stack != nullptr)
8986
{
90-
for (int i = 0; i < mCount; i++)
91-
{
92-
mStack[i] = nullptr;
93-
}
94-
95-
delete mStack;
96-
mStack = nullptr;
97-
mCount = 0;
87+
//for (int i = 0; i < m_count; i++)
88+
//{
89+
// m_stack[i] = nullptr;
90+
//}
91+
92+
delete m_stack;
93+
m_stack = nullptr;
94+
m_count = 0;
9895
}
9996
}
10097

10198
template<typename T>
10299
inline int Stack<T>::Count() const
103100
{
104-
return mCount;
101+
return m_count;
105102
}
106103

107104
template<typename T>
108105
inline int Stack<T>::Capacity() const
109106
{
110-
return mCapacity;
107+
return m_capacity;
111108
}
112109

113110
template<typename T>
114111
inline T& Stack<T>::operator[](int index)
115112
{
116-
return const_cast<T&>(const_cast<const Stack<T>*>(this)[index]);
113+
return const_cast<T&>(static_cast<const Stack<T>&>(*this)[index]);
117114
}
118115

119116
template<typename T>
120117
inline const T& Stack<T>::operator[](int index) const
121118
{
122119
assert(index >= 0 && index < m_count);
123-
return m_content[index];
120+
return m_stack[index];
124121
}
125122

126123
template<typename T>
@@ -131,43 +128,43 @@ namespace Algorithm {
131128
return false;
132129
}
133130

134-
mCapacity = size;
131+
m_capacity = size;
135132

136-
mCount = 0;
133+
m_count = 0;
137134

138-
mStack = new T[mCapacity];
135+
m_stack = new T[m_capacity];
139136

140137
return true;
141138
}
142139

143140
template<typename T>
144141
inline bool Stack<T>::Reallocation()
145142
{
146-
if (mCapacity == 0)
143+
if (m_capacity == 0)
147144
{
148145
return false;
149146
}
150147

151-
mCapacity = 2 * mCapacity;
148+
m_capacity = 2 * m_capacity;
152149

153-
T* newContent = new T[mCapacity];
150+
T* newContent = new T[m_capacity];
154151

155-
for (int i = 0; i < mCount; i++)
152+
for (int i = 0; i < m_count; i++)
156153
{
157-
newContent[i] = mStack[i];
154+
newContent[i] = m_stack[i];
158155
}
159156

160-
delete[] mStack;
157+
delete[] m_stack;
161158

162-
mStack = newContent;
159+
m_stack = newContent;
163160

164161
return true;
165162
}
166163

167164
template<typename T>
168165
inline bool Stack<T>::IsCapacityEnough() const
169166
{
170-
return mCount < mCapacity;
167+
return m_count < m_capacity;
171168
}
172169

173170
}

0 commit comments

Comments
 (0)