我在以下代码中遇到了一个奇怪的(或可能没有)错误:
template<typename T>
class Registrer {
public:
Registrer() {
Registry::register(T::instance);
}
};
template<typename T>
class RegisteringClass {
private:
static const Registrer<T> REGISTRER;
public:
RegisteringClass () { Q_UNUSED(REGISTRER); /* force static instantiation */ }
static const WhatEver *instance() { static T INSTANCE; return &INSTANCE; }
};
template<typename T>
const Registrer<T> RegisteringClass<T>::REGISTRER;
class Something : public WhatEver, RegistringClass<Something> {
}
当模板中有instance
方法时,我收到了以下错误(gcc)
error: 'static const WhatEver* RegisteringClass<T>::instance() [with T = Something]' is inaccessible
static const WhatEver*instance() { static T INSTANCE; return &INSTANCE; }
error: within this context
Registry::register(T::instance);
In instantiation of 'static const WhatEver* RegisteringClass<T>::instance() [with T = Something]':
当instance()
位于Something
时,一切都很好。
我是否错过了一些语法细微之处?或者这是模板限制吗?
答案 0 :(得分:3)
在派生课程时,&#39;私人&#39;是默认的; 否则你必须使用&#39; public&#39;或者&#39;受保护的&#39;您派生的每个类的关键字:
<string name=“hello”>Hello world</string>