jeudi 22 novembre 2018

Call function in thread

class base 
{ 
public: 
    virtual void fun_1() { cout << "base-1\n"; } 
    virtual void fun_2() { cout << "base-2\n"; } 

}; 

class derived : public base 
{ 
public: 
    void fun_1() { cout << "derived-1\n"; } 
    void fun_2() { cout << "derived-2\n"; 
    } 
}; 


class caller
{
    private:
        derived d;
        unique_ptr<base> b = make_unique<derived>(d);

    public:
        void me()
        {
            b->fun_2(); //? How to do this in thread
            // std::thread t(std::bind(&base::fun_2, b), this);
            // t.join();
        }
};

int main() 
{  
    caller c;    
    c.me();
    return 0;
}

I wrote a small program to learn smart pointer and virtual function together. Now I am stuck, How I could call b->fun_2() in a thread, I cannot change base and derived class. I also have to use unique_ptr and cannot change to shared_ptr. Also please explain the error message when you uncomment the line I commented if possible.

Aucun commentaire:

Enregistrer un commentaire