I am confused by the errors generated by the following code . In Derived :: doStuff, I can directly access Base :: output by calling it.
Why can't I create a pointer to output()in the same context that I can call output()?
(I thought that protected / closed is controlled, is it possible to use the name in a certain context, but apparently this is incomplete?)
Is my writing fix callback(this, &Derived::output);instead of the callback(this, Base::output)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()
{
output();
Base::output();
void (Derived::*derivedPointer)() = &Derived::output;
callback(this, &Derived::output);
}
};
int main()
{
Derived d;
d.doStuff();
}
: , , . , , callback Derived, Derived::output, . Derived, Derived, Derived, Base?