Static Smart Pointers - Yay or Nay?

Say I have an object-- we'll called it Obj-- and I want it contain a static pointer to an instance of it's own type. Ex:

class Obj{
    static auto get_active_obj() -> const Obj*;
    auto make_active_obj() -> void;

private:
    static const Obj* active;
};

Now, would it be good practice to use a smart pointer, specifically a shared_ptr, in this scenario? The idea behind using a shared_ptr here is that when the user queries the object with get_active_obj, I can just return a weak_ptr to the active object, and the user could query this to check if the active object has changed/is still valid. Now, question is, is this the most optimal (or even a viable) solution? Ex:

class Obj{
    static auto get_active_obj() -> std::weak_ptr<const Obj>;
    auto make_active_obj() -> void;

private:
    //another concern with this is that I have to provide a "no-OP" 
    //deleter to prevent a segfault when the shared_ptr tries to delete a static    
    //pointer.
    static std::shared_ptr<const Obj> active;
};

int main(){
    Obj o;
    o.make_active_obj();

    auto active = Obj::get_active_obj();
    if(active.expired()){ //is it still active?
        auto active_obj = active.lock();
        //do stuff
    }

}

Conceptual implementation of make_active_obj():

auto make_active_obj()const -> void {
    active.reset(this);
}

destructor:

~Obj(){
    if(this == active.get())
        active.reset();
}

Answers


I would reverse the use of shared and weak_ptr in your program. That is: I would use a static weak_ptr to hold a reference to the current active object, but return a shared_ptr whenever someones calling get_active_obj() in order to work with it.

class Obj{
    static std::shared_ptr get_active_obj();
    void make_active_obj();

private:
    static std::weak_ptr<const Obj> active;
};

int main(){
    Obj o;
    o.make_active_obj();

    auto active = Obj::get_active_obj();
    if(active){ //is it still active?
        //do stuff
    }
}

The reason is that you probably want to be able to destroy/close your active object without having to make another object active before (e.g. before terminating your program). This would also better fit the semantics of your program: When some function wants to work with the active object it has to be sure that the object won't be destroyed while working on it, so this is a (temporary) owning relationship. The class on the other hand probably doesn't care if a object is destroyed or not, as it only provides a reference to other parts of the program, so this is a non-owning relationship.


Need Your Help

Highcharts - redraw() vs. new Highcharts.chart

javascript highcharts

I'm struggling to understand the correct way to update a highcharts chart. Supposing I have rendered a chart, and then I want to update it in some way. For instance, I may want to change the valu...

How can I get the data from the json one by one by using javascript/jquery on click event?

php javascript jquery json

I have the working code which fetches all the records from the json, but how can I make the records available one by one on the click of the button(next/previous)?