例如:
#include <iostream>
#include <cstring>
using namespace std;
struct Square
{
Square()
{
str = new char[10];
strcpy(str, "Hello");
}
~Square()
{
delete str;
}
void print()
{
cout << "str = '" << str << "'" << endl;
}
char * str;
};
class foo
{
public:
typedef const Square & sqcref;
typedef Square & sqref;
foo(sqref && _ref)
{
fooStr = _ref.str;
_ref.str = 0;
}
char * fooStr;
};
int main()
{
Square s;
s.print();
foo f(s);
s.print();
}
Run Code Online (Sandbox Code Playgroud)
输出:
str ='
Hello'str ='
在构造函数中foo,它期望一个rvalue到a Square &,但传入的是一个Square &左值.为什么这样做?
| 归档时间: |
|
| 查看次数: |
72 次 |
| 最近记录: |