Created
May 5, 2016 09:15
-
-
Save Mikle-Bond/cf6339986cf1172a7772d4f2c9e91177 to your computer and use it in GitHub Desktop.
This file contains 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
#include <iostream> | |
#include <cstdlib> | |
#include <algorithm> | |
template <typename T> | |
class DD | |
{ | |
private: | |
using T_ret_type = typename T::ret_type; | |
protected: | |
typename T::ret_type **body_; | |
size_t size_x, size_y; | |
explicit DD(size_t x, size_t y) { | |
// if we got exception here, so there were no changes | |
body_ = new T_ret_type * [x]; | |
// if all is ok, it should be filled with nullptrs. just in case. | |
std::fill_n(body_, x, nullptr); | |
try { | |
for (size_t i = 0; i < x; ++i) | |
body_[i] = new T_ret_type [y] (); | |
} catch (...) { | |
for (size_t i = 0; i < x; ++i) | |
// it is safe to delete it, due to nulls. | |
delete [] body_[i]; | |
throw; | |
} | |
size_x = x, size_y = y; | |
} | |
~DD() { | |
for (int i = 0; i < size_x; ++i) | |
delete [] body_[i]; | |
delete [] body_; | |
} | |
public: | |
class RowLine { | |
T_ret_type *row_; | |
public: | |
RowLine (T_ret_type *row) : row_(row) {} | |
T_ret_type & operator[] (size_t j) { | |
return row_[j]; | |
} | |
}; | |
friend class ::DD<T>::RowLine; | |
RowLine operator[] (size_t i) { | |
return RowLine(body_[i]); | |
} | |
}; | |
class Matrix : public DD<Matrix> | |
{ | |
public: | |
// using ret_type = int; | |
typedef int ret_type; | |
explicit Matrix (size_t x, size_t y) : DD<Matrix>(x, y) {} | |
}; | |
int main () | |
{ | |
Matrix m(5, 5); | |
m[1][3] = 5; | |
std::cout << m[1][3] << std::endl; | |
return 0; | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment