Created
March 8, 2016 11:58
-
-
Save orgads/c88ca56ecf947a76ea8e 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
| #include <new> | |
| template<class T> | |
| class MyArray | |
| { | |
| public: | |
| MyArray(int _Count) : ObjSize(sizeof(T)), Count(_Count) | |
| { | |
| Buf = new char[ObjSize * Count]; | |
| for (int i = 0; i < Count; ++i) | |
| new(Buf + ObjSize * i) T; | |
| } | |
| T &operator[](int Pos) { return *reinterpret_cast<T *>(Buf + ObjSize * Pos); } | |
| private: | |
| char *Buf; | |
| int ObjSize; | |
| int Count; | |
| }; | |
| class Foo { int bar = 42; }; | |
| int main(int argc, char *argv[]) | |
| { | |
| MyArray<Foo> arr(10); | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment