NULL pointer with boost::shared_ptr?

后端 未结 6 1016
悲哀的现实
悲哀的现实 2020-12-12 20:35

What\'s the equivalent to the following:

std::vector vec;
vec.push_back(NULL);

when dealing with boost::shared_ptr

6条回答
  •  攒了一身酷
    2020-12-12 21:13

    Well, this is legal:

    shared_ptr foo;  /* don't assign */
    

    And in this state, it doesn't point to anything. You can even test this property:

    if (foo) {
        // it points to something
    } else {
        // no it doesn't
    }
    

    So why not do this:

    std::vector < shared_ptr > vec;
    vec.push_back (shared_ptr);   // push an unassigned one
    

提交回复
热议问题