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();
}
Edit: I'd love to know where this is in the stardard, but mostly I'm just trying to wrap my head around the concept. I think my problem is that callback
doesn't have access to protected members of Derived
, but it is able to call Derived::output
if you pass it a pointer. How is a protected member of Derived
that comes from Derived
different from a protected member of Derived
that comes from Base
?