【发布时间】:2016-01-28 07:47:29
【问题描述】:
我正在尝试访问从EventListener 模板继承的静态变量,但看起来派生的KeyboardListener 类不是EventDispatcher 的朋友。我做错了什么?
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;
}
};
【问题讨论】:
标签: c++ class templates friend