#include <iostream>
using namespace std;
class Person{
public:
Person()
{
cout<<"Person的无参调用"<<endl;
}
Person(int age)
{
m_age=age;
cout<<"Person的有参调用"<<endl;
}
Person(const Person &p)
{
m_age=p.m_age;
cout<<"Person的拷贝构造函数调用"<<endl;
}
~Person()
{
cout<<"Person的析构函数的调用"<<endl;
}
int m_age;
};
Person test()
{
Person p1;
return p1;
}
void func()
{
Person p = test();
}
int main()
{
func();
}
上述是代码,在visual stdio 2019 运行结果是
而在Linux下是
我又试了其他编译器sublime text 3
vscode同样也是
这是为啥啊
可能是对变量的赋值方式处理不同。只有VS2019下是深拷贝,别的都像是共享空间一样,只有一个实例。
各种编译器的处理方式不一样造成的吧
你把19行的构造函数去掉再看看什么结果