Search code examples
c++pointersnullshared-ptr

shared_ptr null pointer and assignment


I want to use shared_ptr just like I'd use an actual pointer. I wanted to be able to do things like

shared_ptr<int> a;
a = new int(5);
a = 0;
shared_ptr<int> foo()
    return 0;

but it is not implemented by default.

I changed the source code of the shared_ptr of the boost library by adding

template<class Y> void operator=( int i )
{
    reset(i);
}
template<class Y> void reset(int i)
{
    this_type(i).swap(*this);
}
template<class Y> void operator=( Y * p )
{
    reset(p);
}
shared_ptr(int i): px(0), pn()
{
}

The only thing is that if I do a = -1; it will compile and give me a null pointer, which shouldn't be a problem because normally you can't assign an integer value to a pointer.

So my question is, is this a correct way to implement this or have I forgot cases that might crash the application? Because everywhere I looked, the only way I saw to get a nullpointer for a shared_ptr was to call the default constructor which isn't very elegant in code compared to: ptr = 0;.


Solution

  • No. Do not change the source. It's like that for a reason and very smart people have decided that the way it's defined is better than whatever you're going to edit it to be.

    What you have doesn't even make sense. You cannot assign an integer to a pointer, they are two different types, yet you've given it such semantics. You say: "...which shouldn't be a problem because normally you can't assign an integer value to a pointer", but you also said at the top of your question "I want to use shared_ptr just like I'd use an actual pointer". Well, which is it? Because assigning integers to pointers to set them to null is about as far from an actual pointer as you can get.

    You need to take a step back and realize what you want isn't always the best thing to do. You should revert those changes and use the class properly. I don't want to be mean but this is seriously not the route you want to go; it's dangerous, has nonsensical semantics, and is all around unmaintainable.