F
fl
Hi,
I am new to C++. On C++ primer, 4th, page 601, the author said: (for
the below assignement function)
/////////////////
// use-counted assignment operator; use is a pointer to a shared use
count
Sales_item&
Sales_item:
perator=(const Sales_item &rhs)
{
++*rhs.use;
decr_use();
p = rhs.p;
use = rhs.use;
return *this;
}
/////////////////
As usual with an assignment operator, we must protect against self-
assignment. This operator handles self-assignemnt by first
incrementing the use count in the right-hand operand. If the left- and
right-hand operands are the same, the use count will be at least 2
when "decr_use" is called. That function decrements and checks the use
count of the left-hand operand. If the use count goes to zero, then
decr_use will free the "Item_base" and "use" objects currently in this
object. What remains is to copy the pointers from the right-hand to
the left-hand operand. As usual, our assignment operator returns a
reference to the left-hand operand.
...........
My question is: I do not understand the paragraph and conclusion. I
agree that we must protect self-assignment. But, from the above
description, it seems that the above assignment function lacks self-
assignement now. That is, our assignment operator returns a reference
to the left-hand operand, but decr_use may free the "Item_base" and
"use" objects currently in this object.
If it requires self-assignment protection, how to do that? That book
talks the method about self-assignment protection? Please tell me if
you know that. Thanks a lot.
I am new to C++. On C++ primer, 4th, page 601, the author said: (for
the below assignement function)
/////////////////
// use-counted assignment operator; use is a pointer to a shared use
count
Sales_item&
Sales_item:
{
++*rhs.use;
decr_use();
p = rhs.p;
use = rhs.use;
return *this;
}
/////////////////
As usual with an assignment operator, we must protect against self-
assignment. This operator handles self-assignemnt by first
incrementing the use count in the right-hand operand. If the left- and
right-hand operands are the same, the use count will be at least 2
when "decr_use" is called. That function decrements and checks the use
count of the left-hand operand. If the use count goes to zero, then
decr_use will free the "Item_base" and "use" objects currently in this
object. What remains is to copy the pointers from the right-hand to
the left-hand operand. As usual, our assignment operator returns a
reference to the left-hand operand.
...........
My question is: I do not understand the paragraph and conclusion. I
agree that we must protect self-assignment. But, from the above
description, it seems that the above assignment function lacks self-
assignement now. That is, our assignment operator returns a reference
to the left-hand operand, but decr_use may free the "Item_base" and
"use" objects currently in this object.
If it requires self-assignment protection, how to do that? That book
talks the method about self-assignment protection? Please tell me if
you know that. Thanks a lot.