目前来看的区别更好总结如下:
new object
分配了内存空间,初始化不确定
new object()
调用了 object
类的构造函数来进行初始化
当类有了显示化的默认构造函数时,两种 new
都自动调用该默认构造函数
#include<iostream>
#include <unordered_set>
#include <vector>
#include <iomanip>
using namespace std;
class Base1 {
public:
int a;
};
class Base2 {
public:
Base2() {}
int a;
};
class Base3 {
public:
Base3() {
a = 2;
}
int a;
};
int main() {
Base1* b1 = new Base1;
Base1* bb1 = new Base1();
Base2* b2 = new Base2;
Base2* bb2 = new Base2();
Base3* c3 = new Base3;
Base3* cc3 = new Base3();
int* i = new int;
int* ii = new int();
cout << "(new object) b1->a: " << b1->a << '\n'
<< "(new object()) bb1->a: " << bb1->a << '\n'
<< "(new object) b2->a: " << b2->a << '\n'
<< "(new object()) bb2->a: " << bb2->a << '\n'
<< "(new object) c3->a: " << c3->a << '\n'
<< "(new object()) cc3->a: " << cc3->a << '\n'
<< "new int i: " << *i << '\n'
<< "new int() ii: " << *ii << endl;
return 0;
}