Here is a sample code
class A{
int i;
public:
A(int i) : i(i) {}
void f() { prn(i); }
};
int main()
{
A* pi = new A(9);
A* pi2= new A(87);
boost::shared_ptr<A> spi(pi);
boost::shared_ptr<A> spi2(pi2);
spi=spi2;
spi->f();
spi2->f();
pi->f();
pi2->f();
}
output:
87
87
0
87
The question is, why is output 0?
Documentation note: Effects: equivalent to shared_ptr (r) .swap (* this).
But if the objects shared_ptrjust swap, the result should be 9. And if the first object is deleted, there must be a segmentation error.
So why 0?
source
share