Skip to content

Instantly share code, notes, and snippets.

@DarTheStrange
Last active December 17, 2015 20:19
Show Gist options
  • Save DarTheStrange/5666695 to your computer and use it in GitHub Desktop.
Save DarTheStrange/5666695 to your computer and use it in GitHub Desktop.
Templated array.
#include <iostream>
using namespace std;
template<class T>
class array {
int size;
T* g;
public:
array(int size = 8){
this->size = size;
this->g = new T[size];
}
array(const array<T>& a){
this->size = a.size;
this->g = new T[this->size];
for (int i = 0; this->size - i; ++i)
this->g[i] = a.g[i];
}
~array(){
delete[] g;
}
array<T>& operator=(const array<T>& a){
this->size = a.size;
this->g = new T[this->size];
for (int i = 0; this->size - i; ++i)
this->g[i] = a.g[i];
return *this;
}
T& operator[] (int y){
return g[y];
}
const T& operator[] (int y) const{
return g[y];
}
void resize(int y){
T* temp = new T[y];
for (int i = 0; y - i && size - i; ++i){
temp[i] = g[i];
}
delete[] g;
g = temp;
size = y;
}
void append(T t){
resize(size + 1);
(*this)[size - 1] = t;
}
};
int main(){
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment