开发者

Difference between new and operator new in C++ [duplicate]

开发者 https://www.devze.com 2023-02-17 19:09 出处:网络
This question already has answers here: Closed 11 years ago. Possible Duplicate: Difference between 'new operator' and 'op开发者_JAVA百科erator new'?
This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Difference between 'new operator' and 'op开发者_JAVA百科erator new'?

What is the difference between using new and operator new ?

I read somewhere a while ago that operator new behaves like malloc(no constructor). Is this correct? If so can someone show me on example, when trying to allocate an object using "operator new" it gives me error.

Thanks.


operator new is the lowest level allocation mechanism. Actual objects should be allocated with new, which tells C++ to actually create the object and set up all its necessary plumbing (superclass information, vtables, etc.), without which it isn't capable of being an object.


  • operator new doesn't call constructor of class T, and returns void*, not T*.
  • new internally calls operator new, then call the constructor of the class T to construct the object in the allocated memory and then return T*.
0

精彩评论

暂无评论...
验证码 换一张
取 消