默认构造函数
#include <iostream>
using namespace std;
class Person
{
};
int main(int argc, char *argv[])
{
Person p1;
cin.get();
return 0;
}
默认构造函数 执行效果
在栈和堆调用不同
#include <iostream>
using namespace std;
class People
{
public:
People()
{
cout << "构造函数被调用" << endl;
}
~People()
{
cout << "析构函数被调用" << endl;
}
};
void stack_call()
{
People people;
}
void heap_call()
{
People *people = new People;
}
int main(int argc, char *argv[])
{
cout << "在栈上调用,构造和析构都会调用" << endl;
stack_call();
cout << "=========================" << endl;
cout << "在堆上调用,构造会被调用,析构不会被调用" << endl;
heap_call();
cout<<"========================="<<endl;
cin.get();
return 0;
}
在栈和堆调用 效果