Skip to content

Instantly share code, notes, and snippets.

@deepanshululla
Last active December 25, 2021 01:46
Show Gist options
  • Save deepanshululla/0cf6dcf3f1a49e2306be60d7f0199e61 to your computer and use it in GitHub Desktop.
Save deepanshululla/0cf6dcf3f1a49e2306be60d7f0199e61 to your computer and use it in GitHub Desktop.
#include <iostream>
#include <string>
template <class T>
class Array2D {
public:
Array2D(int dim1, int dim2) :d_container(new Array1D(dim1)) {
for (int i=0; i< dim1; ++i) {
Array1D array1(dim2);
*(d_container+i) = array1;
}
};
class Array1D {
public:
Array1D(int dim) :d_data(new T[dim]){
};
Array1D(const Array1D& other) : d_data(other.d_data){};
Array1D& operator=(const Array1D& rhs){
if (d_data != rhs.d_data) {
d_data = rhs.d_data;
}
return *this;
};
T& operator[] (int index) {
return *(d_data+index);
};
const T& operator[] (int index) const {
return *(d_data+index);
};
T* d_data;
};
Array1D operator[] (int index) {
return *(d_container+index);
};
const Array1D operator[] (int index) const {
return *(d_container+index);
};
Array1D* d_container;
};
int main() {
Array2D<int> a(10,30);
a[2][3] = 1;
std::cout << a[2][3] << std::endl;
Array2D<std::string> b(10,30);
b[2][3] = "hello";
std::cout << b[2][3] << std::endl;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment