the operator-> return value of smart pointers

Viewed 705

smart pointers like shared_ptr can be used like ordinary pointers with * and -> operator. The books say that -> operator returns the pointer that shared_ptr stores. So you can use it to access the object this pointer points to. But I am confused here. Look at the code below.

class A
{
public:
    A(int v = 20){val = v;}
    int val;
}
A* p1 = new A;
std::cout<<p1->val;  //This is common sense

boost::shared_ptr<A> p2(new A);
std::cout<<p2->val;  //This is right
//My question is that p2-> returns the pointers of the object, then maybe another 
//-> should be used?
//like (p2->)->val? 
3 Answers
Related