Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;.

like image 440
Michael Ferris Avatar asked Nov 27 '22 01:11

Michael Ferris


2 Answers

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.

like image 181
GManNickG Avatar answered Dec 05 '22 11:12

GManNickG


shared_ptr implements a pointer-like interface so you can use the * and -> operator.

A default-constructed (empty) shared_ptr will be equal to nullptr internally, so you don't need to worry about assigning that explicitly. i.e.

std::shared_ptr<int> sh;
std::cout << ( sh.get() == nullptr ) << std::endl;

// Or alternatively:
std::cout << ( sh == nullptr ) << std::endl;

Also, (in my opinion) the most elegant way to use shared_ptr to create objects is the std::make_shared function. This also has the added benefit of being slightly more efficient in some compilers (at least on MSVC++) due to some internal optimisation known as the "we know where you live idiom"

#include <memory>
#include <string>

class foo
{
public:
    foo(std::string s, int n) {}
};

int main()
{
    auto p = std::make_shared<int>();
    auto q = std::make_shared<foo>("hello", 2);
}
like image 39
Ben Cottrell Avatar answered Dec 05 '22 13:12

Ben Cottrell