#include<iostream>
using namespace std;
class A
{
public:
int x;
A(int b){x=b;};
};
A& f()
{
A &a(1);//为什么改成 A &a=* new A(1);就对了
return a;
}
int main()
{
A a(2);
a=f();
cout<<a.x;
}
...全文
5411打赏收藏
一个很奇怪的问题???
#include using namespace std; class A { public: int x; A(int b){x=b;}; }; A& f() { A &a(1);//为什么改成 A &a=* new A(1);就对了 return a; } int main() { A a(2); a=f(); cout<<a.x; }
[Quote=引用楼主 wind2411hawk 的帖子:]
#include <iostream>
using namespace std;
class A
{
public:
int x;
A(int b){x=b;};
};
A& f()
{
A &a(1);//
return a;
}
int main()
{
A a(2);
a=f();
cout < <a.x;
}
[/Quote]
new出来的在堆上不会随作用域结束而析构所以A &a=* new A(1);就OK