Created
April 21, 2016 04:41
-
-
Save dragonly/9fe7de4fe81c061d5768d76e26e965cb to your computer and use it in GitHub Desktop.
test rvalue related behaviors of C++11
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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