【发布时间】:2011-03-12 00:51:08
【问题描述】:
class A : boost::noncopyable {
int type;
A(int type, enum e) : type(type) { /* ... */ }
}
typedef boost::shared_ptr<A> A_ptr;
template <enum VAR>
class B : boost::noncopyable {
static A_ptr x, y;
B() {
if (!x.get()) {
x.reset(new A(0, VAR));
}
if (!y.get()) {
y.reset(new A(1, VAR));
}
}
}
我想根据模板有几种类型的“B”;这样,它将保持单独的静态和“跟踪”它们自己的x、y (A_ptrs)。
问题:
a) 如何在模板正常工作的情况下将其设置为标头/源配置?
b) 我应该形成一个“createA”函数而不是x.reset(new A(0, VAR));
c) 未初始化时,shared_ptr.get() 是否保证返回 0(即布尔表达式中的 false)?
我应该将 b) 和 c) 拆分为多个 SO 问题吗?
【问题讨论】:
-
注:
shared_ptr不能“未初始化”,如果您不提供初始化程序,它将被默认构造。
标签: c++ design-patterns templates boost shared-ptr