For example:
#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();
}
Output:
str = 'Hello'
str = '
Inside the constructor for foo
, it expects an rvalue to a Square &
, but what is passed in is a Square &
lvalue. Why does this work?