C++ vs Java constructors

前端 未结 14 2685
一个人的身影
一个人的身影 2021-02-09 20:31

According to John C. Mitchell - Concepts in programming languages,

[...] Java guarantees that a constructor is called whenever an object is created.

14条回答
  •  猫巷女王i
    2021-02-09 21:32

    Only When you overload new operator function then constructor is not called (it used to avoid constructor calling), else its in standard that constructor is invoked when object is created.

    void * operator new ( size_t size )
    {
    
         void *p = malloc(size);
    
         if(p)
           return p; 
         else
            cout<(operator new(sizeof(X)*5)); // no ctor called
    
    }
    

提交回复
热议问题