How does boost::shared_ptr/make_shared equality work?

Started by
12 comments, last by phil_t 9 years ago

Hmmm... right... so is that because in that case, o already is a pointer and therefore allocated on the heap, whereas an object allocated on the stack has to be copied into the heap before using a shared_ptr can make any sense and by that time it's obviously referring to a different part of memory?

I hope I explained that right...

EDIT: Whereas raw pointers work because they just point to memory without asking any questions and don't care if they're pointing to stack memory, unlike shared_ptr which specifically avoids it?

Advertisement

For the purpose of explaining it in simple terms: yes, your heap allocated object can be managed by the shared pointer, but the stack allocated object cannot.

Ok... I think now the only major uncertainty I have is with the "shared" nature of shared_ptr.

How does the syntax work for using multiple shared_ptr objects to refer to the same pointer? I think now it must be very different to how I might have imagined it.

If we declare a shared_ptr like this:


boost::shared_ptr< Object > sP1( new Object() );

then is it as simple as:


boost::shared_ptr< Object > sP2 = sP1;

?


How does the syntax work for using multiple shared_ptr objects to refer to the same pointer? I think now it must be very different to how I might have imagined it.

How to create and use share_ptr instances

It's for std::shared_ptr, but it should be nearly identical for the boost version.

Lots of resources on the web for this.

This topic is closed to new replies.

Advertisement