C 中模板类可以有静态成员吗?
C++ 中的模板类可以有静态成员吗?既然它不存在并且在使用之前是不完整的,那么这可能吗?
Can a template class in C++ have static members? Since it doesn't exist and is incomplete before it is used, is this possible?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的。静态成员在
template
内声明或定义。 ……> class { ... }
块。如果已声明但未定义,则必须有另一个声明提供该成员的定义。为模板的每个参数化创建一个单独的静态成员。不可能在模板生成的所有类之间共享单个成员。为此,您必须在模板之外定义另一个对象。部分专业化的特质类可能会有所帮助。
Yes. The static member is declared or defined inside the
template< … > class { … }
block. If it is declared but not defined, then there must be another declaration which provides the definition of the member.A separate static member is created for each parameterization of the template. It is not possible to have a single member shared across all classes generated by the template. For that, you must define another object outside the template. A partially-specialized traits class might help with that.