Fix (and test) that copy constructor decrements previous object counter
This commit is contained in:
parent
4a1a0c8855
commit
2e30a9f407
@ -54,10 +54,18 @@ public:
|
||||
|
||||
rcp<T> & operator=(const rcp & other)
|
||||
{
|
||||
ptr = other.ptr;
|
||||
if (ptr)
|
||||
if (ptr != other.ptr)
|
||||
{
|
||||
ptr->rcp_inc();
|
||||
T * old_ptr = ptr;
|
||||
ptr = other.ptr;
|
||||
if (ptr)
|
||||
{
|
||||
ptr->rcp_inc();
|
||||
}
|
||||
if (old_ptr)
|
||||
{
|
||||
old_ptr->rcp_dec();
|
||||
}
|
||||
}
|
||||
return *this;
|
||||
}
|
||||
|
||||
@ -28,6 +28,7 @@ public:
|
||||
int x;
|
||||
int y;
|
||||
};
|
||||
typedef rcp<MyBase> MyB;
|
||||
|
||||
class MyDerived : public MyBase
|
||||
{
|
||||
@ -85,7 +86,6 @@ void test_booleans()
|
||||
|
||||
void test_create()
|
||||
{
|
||||
typedef rcp<MyBase> MyB;
|
||||
MyB myb = MyB::create(8, 9);
|
||||
assert(myb->x == 8);
|
||||
}
|
||||
@ -124,6 +124,14 @@ void test_multi_construct_from_raw_pointers()
|
||||
}
|
||||
}
|
||||
|
||||
void test_copy_assignment_decrements_previous_reference()
|
||||
{
|
||||
MyB myb = MyB::create(12, 13);
|
||||
MyB myb2 = MyB::create(14, 15);
|
||||
myb = myb2;
|
||||
assert(myb->x == 14);
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
{
|
||||
test_class_hierarchy();
|
||||
@ -131,5 +139,6 @@ int main(int argc, char * argv[])
|
||||
test_booleans();
|
||||
test_create();
|
||||
test_multi_construct_from_raw_pointers();
|
||||
test_copy_assignment_decrements_previous_reference();
|
||||
return 0;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user