fork download
  1. ////=========================================================================================================================!
  2. #include <new>
  3. //===============================================================
  4. class MyClass
  5. {
  6. private :
  7. int ma;
  8. public :
  9. MyClass():ma(-1){}
  10. };
  11. //===============================================================
  12. int main()
  13. {
  14. // I am allocating the memory for holding 10 elements of MyClass on heap
  15. void* pMyClass = ::operator new(sizeof(MyClass)*10);
  16.  
  17. //! Note :: the address of pMyClass1 and pMyClass will now point to same location after calling placement new
  18. MyClass* pMyClass1 = :: new(pMyClass)MyClass();
  19. // Problem with this is that, i can only instantiate the constructor for the base address. that is pMyClass[0].
  20. // If i have to instantiate it for all the other instances, that is pMyClass[1] to pMyClass[9], then how to do it ?
  21. return 0;
  22. }
Success #stdin #stdout 0.01s 2852KB
stdin
Standard input is empty
stdout
Standard output is empty