Does new[] call default constructor in C++?

前端 未结 3 653
一生所求
一生所求 2020-11-29 23:54

When I use new[] to create an array of my classes:

int count = 10;
A *arr = new A[count];

I see that it calls a default constructor of

相关标签:
3条回答
  • 2020-11-30 00:22

    int is not a class, it's a built in data type, therefore no constructor is called for it.

    0 讨论(0)
  • 2020-11-30 00:23

    Built-in types don't have a default constructor even though they can in some cases receive a default value.

    But in your case, new just allocates enough space in memory to store count int objects, ie. it allocates sizeof<int>*count.

    0 讨论(0)
  • 2020-11-30 00:28

    See the accepted answer to a very similar question. When you use new[] each element is initialized by the default constructor except when the type is a built-in type. Built-in types are left unitialized by default.

    To have built-in type array default-initialized use

    new int[size]();
    
    0 讨论(0)
提交回复
热议问题