Difference between new and operator new in C++ [duplicate]
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 classT
, and returnsvoid*
, notT*
.new
internally callsoperator new
, then call the constructor of the classT
to construct the object in the allocated memory and then returnT*
.
精彩评论