I want to make a derived class that can call a base class method using std::async. When I try to do this I get a compiler message saying it is protected, but I am passing "this" as the instance which is a derived class of the function I want to execute.
Here is my MCVE:
#include <iostream>
#include <thread>
#include <future>
class Base
{
protected:
bool threadFunc()
{
std::cout << "threadFunc called" << std::endl;
return true;
}
};
class Derived : public Base
{
public:
void callBaseMethodAsync()
{
std::future<bool> results = std::async(std::launch::async, &Base::threadFunc, this);
}
};
int main()
{
Derived test;
test.callBaseMethodAsync();
}
Which results in this compiler error message with gcc 4.8:
g++ -o source.o -c -std=c++11 source.cpp
source.cpp: In member function 'void Derived::callBaseMethodAsync()':
source.cpp:8:10: error: 'bool Base::threadFunc()' is protected
bool threadFunc()
^
source.cpp:20:75: error: within this context
std::future<bool> results = std::async(std::launch::async, &Base::threadFunc, this);
Why is std::async making a protected method not accessible from the derived class? What are some alternate ways to achieve using async to call the base class method from the derived class?