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