在非模板库类中拥有静态成员的最佳方法是什么,而不必在类用户上承担定义成员的负担?
说我想提供这个课程:
class i_want_a_static_member
{
static expensive_resource static_resource_;
public:
void foo()
{
static_resource_.bar();
}
};
Run Code Online (Sandbox Code Playgroud)
那么类的用户一定不要忘了某处定义静态成员(如已经回答了 很多 次):
// this must be done somewhere in a translation unit
expensive_resource i_want_a_static_member::static_resource_;
Run Code Online (Sandbox Code Playgroud)
我在下面有一个答案,但它有一些缺点.是否有更好和/或更优雅的解决方案?