0
点赞
收藏
分享

微信扫一扫

c++无参构造函数(栈和堆的区别)

迎月兮 2022-05-02 阅读 82
#include <iostream>
using 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()不会被认作无参构造函数,只是函数声明,不然会有二义性

举报

相关推荐

0 条评论