Add move constructor and move assignment operator
This commit is contained in:
1 parent
2e30a9f407
commit
62b729badd
2 files changed
+71
No files matched your search
@@ -132,6 +132,55 @@ void test_copy_assignment_decrements_previous_reference()
|
||||
assert(myb->x == 14);
|
||||
}
|
||||
|
||||
void test_move_constructor()
|
||||
{
|
||||
int constructed_before = mybase_construct;
|
||||
int destructed_before = mybase_destruct;
|
||||
{
|
||||
MyB a = MyB::create(1, 2);
|
||||
MyB b = std::move(a);
|
||||
assert(!a);
|
||||
assert(b);
|
||||
assert(b->x == 1);
|
||||
assert(mybase_destruct == destructed_before);
|
||||
}
|
||||
assert(mybase_construct == constructed_before + 1);
|
||||
assert(mybase_destruct == destructed_before + 1);
|
||||
}
|
||||
|
||||
void test_move_assignment()
|
||||
{
|
||||
int constructed_before = mybase_construct;
|
||||
int destructed_before = mybase_destruct;
|
||||
{
|
||||
MyB a = MyB::create(1, 2);
|
||||
MyB b;
|
||||
b = std::move(a);
|
||||
assert(!a);
|
||||
assert(b);
|
||||
assert(b->x == 1);
|
||||
assert(mybase_destruct == destructed_before);
|
||||
}
|
||||
assert(mybase_construct == constructed_before + 1);
|
||||
assert(mybase_destruct == destructed_before + 1);
|
||||
}
|
||||
|
||||
void test_move_assignment_releases_existing()
|
||||
{
|
||||
int constructed_before = mybase_construct;
|
||||
int destructed_before = mybase_destruct;
|
||||
{
|
||||
MyB a = MyB::create(1, 2);
|
||||
MyB b = MyB::create(3, 4);
|
||||
b = std::move(a);
|
||||
assert(!a);
|
||||
assert(b->x == 1);
|
||||
assert(mybase_destruct == destructed_before + 1);
|
||||
}
|
||||
assert(mybase_construct == constructed_before + 2);
|
||||
assert(mybase_destruct == destructed_before + 2);
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
test_class_hierarchy();
|
||||
@@ -140,5 +189,8 @@ int main(int argc, char * argv[])
|
||||
test_create();
|
||||
test_multi_construct_from_raw_pointers();
|
||||
test_copy_assignment_decrements_previous_reference();
|
||||
test_move_constructor();
|
||||
test_move_assignment();
|
||||
test_move_assignment_releases_existing();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user