Add move constructor and move assignment operator
This commit is contained in:
parent
2e30a9f407
commit
62b729badd
@ -70,6 +70,25 @@ public:
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
rcp(rcp && other) noexcept : ptr(other.ptr)
|
||||||
|
{
|
||||||
|
other.ptr = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
rcp<T> & operator=(rcp && other) noexcept
|
||||||
|
{
|
||||||
|
if (ptr != other.ptr)
|
||||||
|
{
|
||||||
|
if (ptr)
|
||||||
|
{
|
||||||
|
ptr->rcp_dec();
|
||||||
|
}
|
||||||
|
ptr = other.ptr;
|
||||||
|
other.ptr = nullptr;
|
||||||
|
}
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
~rcp()
|
~rcp()
|
||||||
{
|
{
|
||||||
if (ptr)
|
if (ptr)
|
||||||
|
|||||||
@ -132,6 +132,55 @@ void test_copy_assignment_decrements_previous_reference()
|
|||||||
assert(myb->x == 14);
|
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[])
|
int main(int argc, char * argv[])
|
||||||
{
|
{
|
||||||
test_class_hierarchy();
|
test_class_hierarchy();
|
||||||
@ -140,5 +189,8 @@ int main(int argc, char * argv[])
|
|||||||
test_create();
|
test_create();
|
||||||
test_multi_construct_from_raw_pointers();
|
test_multi_construct_from_raw_pointers();
|
||||||
test_copy_assignment_decrements_previous_reference();
|
test_copy_assignment_decrements_previous_reference();
|
||||||
|
test_move_constructor();
|
||||||
|
test_move_assignment();
|
||||||
|
test_move_assignment_releases_existing();
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user