c++ - size_t parameter new operator -


i have point in mind can't figure out new operator overloading. suppose that, have class myclass yet myclass.h myclass.cpp , main.cpp files like;

//myclass.h  class myclass {    public:      //some member functions      void* operator new (size_t size);      void operator delete (void* ptr);      //... };  //myclass.cpp  void* myclass::operator new(size_t size) {    return malloc(size); }  void myclass::operator delete(void* ptr) {    free(ptr); }  //main.cpp  //include files //...  int main() {    myclass* cptr = new myclass();    delete cptr }  

respectively. program running fine. however, thing can't manage understand is, how come new operator can called without parameter while in definition has function parameter "size_t size". there point missing here? thanks.

don't confuse "new expression" "operator new" allocation function. former causes latter. when t * p = new t;, calls allocation function first obtain memory , constructs object in memory. process loosely equivalent following:

void * addr = t::operator new(sizeof(t));    //  rough equivalent of t * p = ::new (addr) t;                      //  "t * p = new t;" means. 

(plus exception handler in event constructor throws; memory deallocated in case.)


Comments

Popular posts from this blog

SPSS keyboard combination alters encoding -

Add new record to the table by click on the button in Microsoft Access -

CSS3 Transition to highlight new elements created in JQuery -