Does malloc create a new instance of the class or not?

This question already has an answer here:

  • What is the difference between new/delete and malloc/free? 15 answers

  • The compiler will translate a call to a member function into a call to a static function, with an implied this parameter pointing to the object. That means the contents of the object, valid or invalid, are not relevant to the whether the call is made.

    This changes if the method is virtual, since the vtable pointer must be valid. The constructor will initialize the vtable pointer. malloc does not call the constructor, it doesn't even know what a constructor is - it's leftover baggage from C.

    Note that this is not specified by the standard, but is how things are typically implemented.


    You can access allocated but uninitialized memory, but you cannot make valid assumptions about the contents. The member functions exist independently of objects, they are just called with this pointing to an uninitialized location.

    链接地址: http://www.djcxy.com/p/13778.html

    上一篇: 为什么我们实际上需要C ++中的私有或受保护的继承?

    下一篇: malloc是否创建类的新实例?