#include <iostream> using namespace std; class Anew { public: int a; Anew() {} }; class Bnew { public: int a; Bnew():a(100) {} }; class Cnew { public: int a; }; int main(int argc, char *argv[]) { Anew tmp1; Anew* tmp2 = new Anew; Anew* tmp3 = new Anew(); cout << tmp1.a<<endl; cout << tmp2->a<<endl; cout << tmp3->a<<endl; Bnew tmp11; Bnew* tmp22 = new Bnew; Bnew* tmp33 = new Bnew(); cout << tmp11.a<<endl; cout << tmp22->a<<endl; cout << tmp33->a<<endl; Cnew tmp111; Cnew* tmp222 = new Cnew; Cnew* tmp333 = new Cnew(); cout << tmp111.a<<endl; cout << tmp222->a<<endl; cout << tmp333->a<<endl; return 0; }
g++ 8.1.0
输出:
0
-1163005939
-1163005939
100
100
100
2046
-1163005939
0
Anew 手动写了构造函数
Bnew 手动写了构造函数并有数值初始化
Cnew 编译器自带的构造函数
原文:https://www.cnblogs.com/fundou/p/11100080.html