Skip to content

Instantly share code, notes, and snippets.

@dragonly
Created April 21, 2016 04:41
Show Gist options
  • Save dragonly/9fe7de4fe81c061d5768d76e26e965cb to your computer and use it in GitHub Desktop.
Save dragonly/9fe7de4fe81c061d5768d76e26e965cb to your computer and use it in GitHub Desktop.
test rvalue related behaviors of C++11
#include <iostream>
using namespace std;
class Foo
{
public:
Foo() { cout << "Foo::ctor" << endl; }
Foo(Foo &foo) { cout << "Foo:copy" << endl; }
~Foo(){ cout << "Foo::dtor" << endl; }
};
class A
{
public:
Foo *pf;
A() { cout << "A::ctor" << endl; pf = new Foo(); }
A(A &&a) { cout << "A::moveCto" << endl; pf = a.pf; a.pf = NULL; }
// A(A const &a) { cout << "A::copy" << endl; pf = new Foo(*(a.pf)); }
A &operator =(A &&a) { cout << "A::moveOp=" << endl; Foo *pfTemp = pf; pf = a.pf; a.pf = pfTemp; return *this;}
~A(){ cout << "A::dtor" << endl; delete pf; }
};
A func()
{
A a;
return a;
}
int main()
{
// A b = func();
// A a(func());
// A b;
// A a(move(b));
// printf("%p\n", b.pf);
A a, b;
a = move(b);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment