
Ensure a class has only n instances,and provIDe a global point of access to them.
这是我到目前为止的代码:
template<typename Derived,size_t n = 1>class n_ton_base // Singletons are the default{ static Derived instances[n + (n == 0)]; // Zerotons are supported,tooprotected: // Prevent n_ton_base to be used outsIDe of inheritance hIErarchIEs n_ton_base() {} // Prevent n_ton_base (and Derived classes) from being copIEd n_ton_base(const n_ton_base&) = delete;public: // Get first element by default,useful for Singletons template<size_t i = 0> static Derived& get_instance() { static_assert(i < n,"Time to increase n it seems!"); return instances[i]; }}; 以下是人们如何使用它:
class SingletonExample : public n_ton_base<SingletonExample>{public: voID method() { std::cout << "Singletons are overused.\n"; } };class DoubletonExample : public n_ton_base<DoubletonExample,2>{public: voID method() { std::cout << "Doubleton " << this << " says hello.\n"; } };int main(){ SingletonExample::get_instance().method(); DoubletonExample::get_instance().method(); DoubletonExample::get_instance<0>().method(); DoubletonExample::get_instance<1>().method();} 不幸的是,代码还没有编译:
/tmp/ccsFtliS.o: In function `SingletonExample& n_ton_base<SingletonExample,1ul>::get_instance<0ul>()':nton.cpp:(.text._ZN10n_ton_baseI16SingletonExampleLm1EE12get_instanceILm0EEERS0_v[SingletonExample& n_ton_base<SingletonExample,1ul>::get_instance<0ul>()]+0x5): undefined reference to `n_ton_base<SingletonExample,1ul>::instances'/tmp/ccsFtliS.o: In function `DoubletonExample& n_ton_base<DoubletonExample,2ul>::get_instance<0ul>()':nton.cpp:(.text._ZN10n_ton_baseI16DoubletonExampleLm2EE12get_instanceILm0EEERS0_v[DoubletonExample& n_ton_base<DoubletonExample,2ul>::get_instance<0ul>()]+0x5): undefined reference to `n_ton_base<DoubletonExample,2ul>::instances'/tmp/ccsFtliS.o: In function `DoubletonExample& n_ton_base<DoubletonExample,2ul>::get_instance<1ul>()':nton.cpp:(.text._ZN10n_ton_baseI16DoubletonExampleLm2EE12get_instanceILm1EEERS0_v[DoubletonExample& n_ton_base<DoubletonExample,2ul>::get_instance<1ul>()]+0x5): undefined reference to `n_ton_base<DoubletonExample,2ul>::instances'collect2: ld gab 1 als Ende-Status zurück
我做错了什么?
解决方法 作为 Etienne Cordonnier pointed out,使用本地静态而不是静态类更容易:template<typename Derived,size_t n = 1>class n_ton_base // Singletons are the default{protected: // Prevent n_ton_base to be used outsIDe of inheritance hIErarchIEs n_ton_base() {} // Prevent n_ton_base (and Derived classes) from being copIEd n_ton_base(const n_ton_base&) = delete;public: // Get first element by default,"Time to increase n it seems!"); static Derived instance; return instance; }}; 请注意,每个实例化的成员函数都有自己的本地静态,因此不需要任何数组.
这也实现了thread-safe lazy initialization,而我不必对此做任何事情.太好了!
总结以上是内存溢出为你收集整理的c – 工业强度的n吨基类模板全部内容,希望文章能够帮你解决c – 工业强度的n吨基类模板所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)