error: 'void Base::output()' is protected within this context
- by Bill
I'm confused about the errors generated by the following code.
In Derived::doStuff, I can access Base::output directly by calling it.
Why can't I create a pointer to output() in the same context that I can call output()?
(I thought protected / private governed whether you could use a name in a specific context, but apparently that is incomplete?)
Is my fix of writing callback(this, &Derived::output); instead of callback(this, Base::output) the correct solution?
#include <iostream>
using std::cout; using std::endl;
template <typename T, typename U>
void callback(T obj, U func)
{
((obj)->*(func))();
}
class Base
{
protected:
void output() { cout << "Base::output" << endl; }
};
class Derived : public Base
{
public:
void doStuff()
{
// call it directly:
output();
Base::output();
// create a pointer to it:
// void (Base::*basePointer)() = &Base::output;
// error: 'void Base::output()' is protected within this context
void (Derived::*derivedPointer)() = &Derived::output;
// call a function passing the pointer:
// callback(this, &Base::output);
// error: 'void Base::output()' is protected within this context
callback(this, &Derived::output);
}
};
int main()
{
Derived d;
d.doStuff();
}