shared_ptr null pointer and assignment

27,768

Solution 1

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.

Solution 2

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);
}
Share:
27,768
Michael Ferris
Author by

Michael Ferris

Updated on April 22, 2020

Comments

  • Michael Ferris
    Michael Ferris about 4 years

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

  • Michael Ferris
    Michael Ferris about 12 years
    Yeah, you're right. I guess I was just being lazy because I didn't want to change everything because I had normal pointer before and was changing to smart ptr. I suspected what I was doing was wrong and this is why I asked :)
  • GManNickG
    GManNickG about 12 years
    @MichaelFerris: That's alright, but sometimes you just have to get down to work, especially when refactoring code. :) As your coding style improves you'll find that it's better to be explicit about things. While implicit functionality means less work for you, it usually means less clarity and safety for you. Just looking at return 0;, for example, you'd think you're returning the number zero, not a null pointer. But there's no confusion when you say return boost::shared_ptr<int>();. In fact, this is so much so that the new C++ standard introduced a new keyword just for null: nullptr.
  • Michael Ferris
    Michael Ferris about 12 years
    I didn't even know the existence of nullptr. Thanks! :) Also, I chose to use the shared_ptr in the standard lib so I don't have to carry a huge library like boost. Still so many things to learn ;)
  • GManNickG
    GManNickG about 12 years
    @MichaelFerris: If you can use it, definitely get in the habit. All mainstream compilers now support it, and it's quite useful. Also, welcome to StackOverflow.