Skip to content

Instantly share code, notes, and snippets.

@orgads
Created March 8, 2016 11:58
Show Gist options
  • Select an option

  • Save orgads/c88ca56ecf947a76ea8e to your computer and use it in GitHub Desktop.

Select an option

Save orgads/c88ca56ecf947a76ea8e to your computer and use it in GitHub Desktop.
#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