C ++初始化类静态数据成员 [英] C++ Initialize class static data member
本文介绍了C ++初始化类静态数据成员的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类有一些静态函数来执行一些计算。但是,在计算之前,我需要传递一个数据来初始化一些静态数据成员。目前我有一个init(数据)函数和clearResource()函数,应该在使用类之前和之后调用。有更好的方法吗?
I have a class which has a number of static function to perform some calculation. However, before the calculation, I need to pass in a data to initialize some of the static data members. Currently I have an init(data) function and a clearResource() function which should be called before and after the use of the class. Is there a better way of doing that?
例如:
classA(){
static int a;
static init(int b) {
a = b;
}
static functionA(){
//perform something based on value of a;
switch(a){
}
}
}
int main(){
classA::init(5);
classA::functionA();
}
感谢
推荐答案
您可以使用这种设计
class A()
{
public:
static int a;
static void functionA(int arg = A::a)
{
if(A::a != arg)
A::a = arg;
...
}
};
int A::a = 0;
int main()
{
A::functionA();
}
这篇关于C ++初始化类静态数据成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文