确保静态成员最多设置一次 (C++)
ensure static members are set at most once (C++)
class Foo {
public:
static int& num() {
static int aNum = 0; // initial value
return aNum;
}
static void writer_func() { /* ... do stuff and assign a value to num() */ }
// a bunch of functions that "read" num()
static void reader_func1() { /* uses num() in read-only mode */}
static void reader_func2() { /* uses num() in read-only mode */}
}
如何确保所有reader_func*()
函数都不会为num()
赋值?
附录:在评论/答案中,一些人建议我简单地将该字段声明为一个简单的static int num;
。这是行不通的,因为它可能会违反 ODR
我不得不采用这种略显"奇怪"的模式的原因是不要违反 ODR
另外,这里的限制是 writer_func(( 将被其他代码调用,我不能先发制人地调用 writer_func(( 来初始化值
它
不是特别优雅,但是由于您提到writer_func
可能会被多次调用,因此您可以执行以下操作:
class Foo {
public:
static const int& num();
static void writer_func();
// a bunch of functions that "read" num()
static void reader_func1() { /* uses num() in read-only mode */}
static void reader_func2() { /* uses num() in read-only mode */}
};
class Num {
static int& num() {
static int aNum = 0; // initial value
return aNum;
}
friend const int& Foo::num();
friend void Foo::writer_func();
};
const int& Foo::num() {
return Num::num();
}
void Foo::writer_func() {
/* ... do stuff and assign a value to num() */
}
处理它的一种方法是创建一个允许一次性修改值的类并使用类的实例。
class OneTimeWrite
{
public:
OneTimeWrite() : value_(0), isset_(false) {}
int get() const { return value_; };
void set(int v)
{
if ( isset_ )
{
throw std::runtim_error("Value must not be set more than once.");
}
isset_ = true;
value_ = val;
}
private:
int value_;
bool isset_;
}
class Foo
{
public:
static OneTimeWrite aNum;
// ...
}
要确保需要创建单例类并仅允许修改一次,请执行以下操作:
class StaticContainer
{
public:
static void setValue(int val){
if (!isSet){
isSet=true;
value=val;
std::cout<<"value changed to "<<val<<std::endl;
}
else{
std::cout<<"value cannot be changedn";
}
}
private:
static bool isSet;
static int value;
};
bool StaticContainer::isSet=false;
int StaticContainer::value;
int main()
{
StaticContainer::setValue(10);
StaticContainer::setValue(20);
}
输出:
值更改为 10
无法更改值
如果我们谈论的是编译时值,那么aNum
成为类范围的static const
,例如:
class Foo {
public:
static const int aNum;
static const int& num() {
return aNum;
}
static int writer_func() { /* ... do stuff and return a value, e.g.: */ return 1; }
// a bunch of functions that "read" num()
static void reader_func1() { /* uses num() in read-only mode */}
static void reader_func2() { /* uses num() in read-only mode */}
};
const int Foo::aNum = Foo::writer_func();
与 George 的建议类似的概念,但有一个更整洁/不太复杂的权衡:将num()
包装在只读版本中,并防止(通过额外的类时髦或静态编码规则(使用来自reader_func*()
的num()
。
class Foo {
public:
static int& num() {
static int aNum = 0; // initial value
return aNum;
}
static int num_ro() {
int &aNum = num();
return aNum;
}
static void writer_func() { /* ... do stuff and assign a value to num() */ }
// a bunch of functions that "read" num()
static void reader_func1() { /* uses num_ro() in read-only mode */}
static void reader_func2() { /* uses num_ro() in read-only mode */}
};
相关文章:
- 如何为非常量和常量重载实现一次成员函数?
- 有没有一种快速的方法可以将类的所有静态成员归零?
- 仅在函数中设置静态变量一次
- (2 问题)"类"类型重新定义(即使 #pragma 一次),以及静态函数内的静态成员对象初始化?
- 常量静态成员函数:有另一种方法可用吗?
- 为什么调用 cout.operator<<(const char*) 打印地址而不是字符串? 如何创建一个函数本地静态"HashSet<char>"并初始化它一次?
- C++一次将函数标记为静态就足够了?
- 确保静态成员最多设置一次 (C++)
- 在功能静态变量上调用Setter一次
- 一次启用 MANY 类的成员字段,具体取决于模板<T>
- 有没有一种方法可以使全局函数/静态成员函数一次可呼出
- 为什么一个成员函数只存在一次,甚至在一个包含多个的.h文件中定义?
- 尝试保存到成员向量的连接-一次只保存一个
- 类成员的C++静态成员变量实例化了两次
- 静态成员变量初始化两次
- 如何在不使用静态变量的情况下从递归函数中只调用另一个函数一次
- 我们如何才能只初始化一次结构成员,并确保它们不可变
- 在cuda中使用静态成员函数模板结构的另一种方法
- 赢得API,每X秒更新一次静态控制
- c++只允许成员变量被设置一次