64,649
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class A
{
public:
A(const char *str = "hello") : s(str)
{
}
//private:
string s;
};
const A &func(const A &ref)
{
return ref;
}
int main()
{
A a("this is ok!");
const A &ref1 = func(a);
const A &ref2 = func("this is ko!");//这里调用的时候是传递了一个临时对象
cout<<ref1.s<<endl;//这里没有问题
cout<<ref2.s<<endl;//这里是有问题的,在DEV下面没有任何显示
system("pause");
return 0;
}