The question is already discussed here [a link] Does new[] call default constructor in C++?
Being a beginner and a self-learner, will appreciate positive comments kindly pls don't generalize it as broad,narrow and etc
When we 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.
Here it is said that the objects are initialized by the default constructor what does it mean? Is it like every object has now key initialized to zero(by the default constructor) because that is true when I printed p[2].key?
How can i check if the default constructor initializes an array or not being an int array it can't access the member key in main?
Also (i) prints garbage value but (ii) prints 0;
class People
{
public:
int key;
People()
{
key=0;
}
};
int main()
{
int count=5;
People *p=new People[count];
cout<< p[2].key;
// int *arr=new int[count]; ...(i)
//cout<<*(arr+2);
//int *arr=new int[count](); ... (ii)
//cout<<*(arr+2)<<endl;
return 0;
}