Guest User

Untitled

a guest
Jul 23rd, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include "Allocator.h"
  2.  
  3. class MyClassA {
  4. protected:
  5. int i = 0;
  6. MyAllocator& allocator;
  7. public:
  8. MyClassA(MyAllocator& allocator):
  9. allocator(allocator) {
  10. throw "Fail";
  11. }
  12.  
  13. virtual ~MyClassA() {
  14. cout << "MyClassA::~MyClassA()" << endl;
  15. };
  16.  
  17. void* operator new(size_t size, MyAllocator& allocator) {
  18. cout << "MyClassA::operator new(size_t size, MyAllocator& allocator)" << endl;
  19. cout << "size: " << size << " sizeof(MyClassA): " << sizeof(MyClassA) << endl;
  20. return allocator.allocate(size);
  21. }
  22.  
  23. void operator delete(void *ptr, MyAllocator& allocator) {
  24. cout << "MyClassA::operator delete(void *ptr, MyAllocator& allocator)" << endl;
  25. cout << "sizeof(MyClassA): " << sizeof(MyClassA) << endl;
  26. allocator.deallocate(ptr, sizeof(MyClassA));
  27. }
  28.  
  29. void operator delete(void *ptr, size_t size) {
  30. cout << "MyClassA::operator delete(void *ptr, size_t size)" << endl;
  31. cout << "size: " << size << " sizeof(MyClassA): " << sizeof(MyClassA) << endl;
  32. static_cast<MyClassA*>(ptr)->allocator.deallocate(ptr, size);
  33. }
  34. };
  35.  
  36. int main() {
  37. MyAllocator* allocator = new MyAllocator();
  38.  
  39. try {
  40. MyClassA* a = new(*allocator) MyClassA(*allocator); // C4291
  41. }
  42. catch (...) {
  43. }
  44. return 0;
  45. }
Add Comment
Please, Sign In to add comment