Created
June 3, 2015 12:36
-
-
Save s25g5d4/2d7b6e4c54abbeb3a1cc to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef __VECTOR_H__ | |
#define __VECTOR_H__ | |
#include <iostream> | |
#include "Trace.h" | |
using std::cout; | |
using std::endl; | |
template<class T> | |
class vector | |
{ | |
private: | |
T* v; | |
int sz; | |
static int count; | |
public: | |
explicit vector(int s) : sz(s) | |
{ | |
TRACE(dummy, "vector<T>::vector(int)"); | |
v = new T[s]; | |
cout << " count = " << ++count << endl; | |
} | |
~vector() | |
{ | |
TRACE(dummy, "vector<T>::~vector"); | |
delete [] v; | |
cout << " count = " << count-- << endl; | |
} | |
T& elem(int i) | |
{ | |
return v[i]; | |
} | |
T& operator[](int i) | |
{ | |
return v[i]; | |
} | |
}; | |
template<> | |
class vector<void *> | |
{ | |
private: | |
void **p; | |
int sz; | |
static int count; | |
public: | |
explicit vector(int s) : sz(s) | |
{ | |
TRACE(dummy, "vector<void*>::vector(int)"); | |
p = new void *[s]; | |
cout << " count = " << ++count << endl; | |
} | |
~vector() | |
{ | |
TRACE(dummy, "vector<void*>::~vector"); | |
delete [] p; | |
cout << " count = " << count-- << endl; | |
} | |
void *& elem(int i) | |
{ | |
return p[i]; | |
} | |
void *& operator[](int i) | |
{ | |
return p[i]; | |
} | |
}; | |
template<class T> | |
class vector<T*> : private vector<void *> | |
{ | |
public: | |
typedef vector<void *> Base; | |
explicit vector(int i) : Base(i) | |
{ | |
TRACE(dummy, "vector<T*>::vector(int)"); | |
} | |
~vector() | |
{ | |
TRACE(dummy, "vector<T*>::~vector"); | |
} | |
T*& elem(int i) | |
{ | |
return reinterpret_cast<T*&>(Base::elem(i)); | |
} | |
T*& operator[](int i) | |
{ | |
return reinterpret_cast<T*&>(Base::operator[](i)); | |
} | |
}; | |
template<class T> | |
int vector<T>::count = 0; | |
int vector<void *>::count = 0; | |
#endif |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment