How operator new calls the constructor of class?

前端 未结 3 409
Happy的楠姐
Happy的楠姐 2021-01-13 09:45

I know that, new operator will call the constructor of class.

But how it\'s happening, what is ground level techniques used for this.

3条回答
  •  星月不相逢
    2021-01-13 10:11

    The compiler generates machine code for that. When the compiler sees

    CSomeClass* object = new CSomeClass();
    

    (new statement) it generates code that calls the appropriate operator new() (which allocates memory), calls the right constructor, calls destructors of all fully constructed subobjects in case of exception, calls operator delete() in case an exception occurs during construction. All this is done by extra machine code generated by the C++ compiler for that simply looking statement.

提交回复
热议问题