c++ - C++11 - How to push this object into priority_queue with vector of shared_ptr? -


i have base class priority_queue this:

class base {    //...    std::priority_queue<std::shared_ptr<obj>, std::vector<std::shared_ptr<obj>>, obj_less> obj_queue;    //... } 

on obj class, have method should push object priority_queue:

void obj::set () {     baseserver& myobj = basefactory::getbase();     myobj.set(this); //<------ won't compile :( } 

and set() call set() on base class:

void base::set(const obj& o) {     obj_queue.push(o); } 

i want use this, pointer same obj, , push vector, inside priority_queue....

but won't compile, , i'm bit lost...

any ideas i'm missing here?

you shouln't this, since, it's bad idea , have no problems , if has raw pointer on obj in place of calling set function. idea of code strange, but, it's better use shared_ptr , enable_shared_from_this.

class obj : public std::enable_shared_from_this<obj> { public:    // ...    void set()    {       baseserver& myobj = basefactory::getbase();       myobj.set(std::shared_from_this()); //<------ won't compile :(    } }; 

and baseserver should have function set, receives shared_ptr on obj. , of course should use shared_ptr<obj> in code, calls set. example this

class obj : public std::enable_shared_from_this<obj> { private:    obj() {} public:    static std::shared_ptr<obj> create()    {       return std::make_shared<obj>();    }    // rest code };  // code, calls set function auto object = obj::create(); object->set(); 

Comments