#includeusing namespace std; class Test { public: Test() { cout << "T constructor" << endl; } ~Test() { cout << "T destructor" << endl; } }; Test t2() { Test *p3 = new Test; cout << "t2 function" << endl; return *p3; } int main() { cout << "heap" << endl; //堆对象 的无参构造 函数 2者都可以 Test *p = new Test(); delete p; p = nullptr; Test *p2 = new Test; delete p2; p2 = nullptr; cout << endl; cout << "stack" << endl; Test t1; cout << "t1 end" << endl; Test t2(); //此时带括号的 只是做了一个声明 函数返回值是一个Test的对象 ,函数名字为t2 ps:一般是讲其写到其他文件 然后在此处声明,此时仅做演示 t2(); cout << "t2 end" << endl; return 0; }
所以 Test t2()不会被认作无参构造函数,只是函数声明,不然会有二义性
2.基础类型new 只会申请空间,加上小括号后才会初始化为0(可能有些编译器 会优化为0,不过我们还是需要自己加,不要依赖编译器)



