I'm trying to access static variable inherited from the EventListener
template, but it looks like the derived KeyboardListener
class is not a friend of EventDispatcher
. What am I doing wrong?
template <class T>
class EventListener
{
public:
friend class EventDispatcher;
private:
static int variable;
};
template <class T> int EventListener<T>::variable;
class KeyboardListener : EventListener<KeyboardListener> {};
class EventDispatcher {
public:
static void foo() {
// this works
std::cout << &EventListener<KeyboardListener>::variable << std::endl;
// fails to compile with:
// 'int EventListener<KeyboardListener>::variable' is private
std::cout << &KeyboardListener::variable << std::endl;
}
};
The default inheritance of classes is private. When you have
class KeyboardListener : EventListener<KeyboardListener> {};
You are inheriting privately from EventListener
all the EventListener
members are private. I think you ment to inherit publicly like
class KeyboardListener : public EventListener<KeyboardListener> {};