I'm writing my own version of <vector> because my compiler eVC++ 3.0 does not support any STL templates, or exception handling. The problem I'm having is I can't get it to call the class's copy constructor when reallocating the array. It does the normal stuff, allocate temp variable then copy original array into new temp array.
The blue colored line is where I expect the class's copy constructor to be called.Code:typedef unsigned long size_type; ... ... T* pTemp = new T[nNewSize]; for(size_type i = 0; i < nOldSize; i++) pTemp[i] = m_array[i]; delete[] m_array; m_array = pTemp; ... ... // vector's data items T* m_array; size_type m_nItems; size_type m_nCapacity; size_type m_nBlockSize;
The text class is pretty simple. I put a breakpoint inside the copy constructor but the program never breaks there.
I'm using VC++ 6.0 on XP to develop the template because it's easier than eVC++ 3.0.Code:class CMyClass { public: CMyClass(); ~CMyClass() {} CMyClass(const CMyClass& c); protected: int m_x; int m_y; }; CMyClass::CMyClass() { m_x = 0; m_y = 0; } CMyClass::CMyClass(const CMyClass& c) // This is the copy constructor (I hope) { m_x = c.m_x; m_y = c.m_y; }
Here is the whole thing if anyone is interested. It hasn't been fully tested so there may be other errors. Also haven't done anything about an iterator -- yet.
Code:#ifndef VECTOR_H__E038A84A_C63C_4EDD_B79B_86E9DF752DBE__INCLUDED_ #define VECTOR_H__E038A84A_C63C_4EDD_B79B_86E9DF752DBE__INCLUDED_ typedef unsigned long size_type; namespace mystd { const int InitialBlockSize = 10; template<class T> T min(T n1, T n2) { return (n1 < n2) ? n1 : n2; } template<class T> class vector { public: vector() { m_array = 0; m_nItems = 0; m_nCapacity = 0; m_nBlockSize = InitialBlockSize; } vector(size_type nItems) { m_array = 0; m_nItems = 0; m_nCapacity = 0; m_nBlockSize = InitialBlockSize; Alloc(nItems); } vector(size_type n, const T& t) { m_array = 0; m_nItems = 0; m_nBlockSize = InitialBlockSize; m_nCapacity = 0; Alloc(n); for(size_type i = 0; i < n; i++) array[i] = t; } vector(const vector& v) // copy constructor { m_array = 0; m_nItems = 0; m_nCapacity = 0; m_nBlockSize = InitialBlockSize; Alloc(v.m_nItems); for(size_type i = 0; i < n; i++) array[i] = v.m_array[i]; } T& front() {return m_array[0];} T& back() {return m_array[m_nItems-1];} size_type begin() {return 0;} size_type end() {return m_mItems;} void erase(size_type beg = -1, size_type end = -1) { if(beg == -1) beg = 0; if(end == -1) end = m_nItems; if(beg == 0 && end == m_nItems) { } } ~vector() { if(m_array != 0) delete[] m_array; } void resize(size_type nItems) { if(m_array == 0) { m_array = new T[nItems]; m_nItems = nItems; m_nCapacity = nItems; } else { m_nCapacity = nItems; T* pTemp = new T[nItems]; size_type n = min(m_nItems,nItems); for(size_type i = 0; i < n; i++) pTemp[i] = m_array[i]; delete[] m_array; m_array = pTemp; } m_nItems = nItems; } void push_back(const T& obj) { Alloc(m_nItems + 1); m_array[m_nItems++] = obj; } T& operator[](size_type index) { return m_array[index]; } T& at(size_type index) { return m_array[index]; } size_type size() const {return m_nItems;} size_type capacity() const {return m_nCapacity;} private: void Alloc(size_type nItems) { // do nothing if there is room for // nItems number of elements in the array if(nItems < m_nCapacity) return; // current number of items + number of new // items exceeds current capacity. So we need to // enlarge the array size_type nNewCapacity = m_nCapacity; while( nItems > nNewCapacity) { nNewCapacity += m_nBlockSize; } if(nNewCapacity == nItems) nNewCapacity += m_nBlockSize; T* pTemp = new T[nNewCapacity]; if(m_nItems > 0) { for(size_type i = 0; i < m_nItems; i++) pTemp[i] = m_array[i]; delete[] m_array; } m_array = pTemp; m_nCapacity = nNewCapacity; } T* m_array; size_type m_nItems; size_type m_nCapacity; size_type m_nBlockSize; }; }; // namespace std; #endif // VECTOR_H__E038A84A_C63C_4EDD_B79B_86E9DF752DBE__INCLUDED_



LinkBack URL
About LinkBacks



I used to be an adventurer like you... then I took an arrow to the knee.