Using std :: thread to call overriden method from derived class
I'm trying to get something like this to work:
int main()
{
class Base
{
public:
Base() = default;
virtual void print() = 0;
void CallThread()
{
std::thread(&Base::print, *this);
};
};
class derived1 : public Base
{
public:
derived1() = default;
void print() { printf("this is derived1\n"); }
};
class derived2 : public Base
{
public:
derived2() = default;
void print() { printf("this is derived2\n"); }
};
Base* ptr;
ptr = new derived1();
ptr->CallThread();
return 0;
}
As a result, I want:
it is derivative1.
The problem is, it won't even compile. I am using VisualStudio 2013.
Mistake:
Error 2 errors C3640: 'main :: Base :: [thunk]: __thiscall
main'::
2' :: Base :: `vcall '{0, {flat}}'} '': a reference or virtual member function of the local class must be defined
Any ideas how to make this work?
edit: just to be clear, what I am trying to do in a stream is harder, this is just an example of my program structure (so lambda is not a good idea for me)
+3
source to share