① new 对象时如果后面有():
如果有自定义默认构造函数则会调用;如果没有则会调用编译器合成的默认构造函数,对内存初始化
struct Student
{
int a;
char * p;
};
void foo()
{
Student* pStu1 = new Student; // a 和 p 值不会被初始化
Student* pStu2 = new Student();// a = 0 , p = Null
}
② new 对象时如果后面没有():
如果有自定义默认构造函数则会调用; 如果没有则不会调用;
struct Student
{
Student()
{
a = 50;
p = "Hello World";
}
int a;
const char* p;
};
void foo()
{
Student* pStu1 = new Student; // a = 50 p = "Hello World";
Student* pStu2 = new Student();
}