在实例之间分离类成员函数内的静态变量
MyClass::Foo()
{
static bool isFirst = true;
if (isFirst)
do something;
isFirst = false;
}
MyClass::Bar()
{
static bool isFirst = true;
if (isFirst)
do something;
isFirst = false;
}
我已经使用了上述结构,到目前为止,当我只使用该类的一个实例时,它运行良好。
问题是 MyClass 的所有实例似乎都共享静态变量。
如何使变量不被不同实例共享(但在同一实例之间共享)?
我是否需要维护一个单独的数据结构来将实例存储在某处?
或者可以通过巧妙地使用 C++ 语法来完成此操作吗?
编辑
我忘了提及我在许多函数中都有这样的变量。
添加了 MyClass::Bar() 在那里。
我希望有一种方法无需将 isFirstForFoo、isFirstForBar 等定义为类成员变量,因为实在太多了。
我的实际代码如下所示,
BookInfoVector_t DBProcess_GET_BOOK::SelectBookList()
{
const char* query = "some query statement";
static nsl::SQLitePreparedStatement preparedStatement = nsl::SQLitePreparedStatement(static_cast<nsl::SQLiteConnection*>(mDBConnection), query);
static bool isFirst = true;
_InitializeDBProcess(&preparedStatement, isFirst);
...
}
我在第一次运行代码时对 preparedStatement 进行了一些初始化,正如您可以想象的那样,我必须为我使用的所有查询定义 isFirst 。
MyClass::Foo()
{
static bool isFirst = true;
if (isFirst)
do something;
isFirst = false;
}
MyClass::Bar()
{
static bool isFirst = true;
if (isFirst)
do something;
isFirst = false;
}
I have used the above structure and it worked well so far when I worked with only one instance of the class.
The problem is all instances of MyClass seem to share the static variable.
How can I make the variable not-shared by different instances(but shared among same instance)?
Do I need to maintain a separate data structure to store instances somewhere?
Or could this be done with clever use of c++ syntax?
edit
I forgot to mention I have such variables in many functions.
Added MyClass::Bar() up there.
I hope there's a way without defining isFirstForFoo, isFirstForBar, and so on as class member variables, because there are so many.
My actual code looks like this
BookInfoVector_t DBProcess_GET_BOOK::SelectBookList()
{
const char* query = "some query statement";
static nsl::SQLitePreparedStatement preparedStatement = nsl::SQLitePreparedStatement(static_cast<nsl::SQLiteConnection*>(mDBConnection), query);
static bool isFirst = true;
_InitializeDBProcess(&preparedStatement, isFirst);
...
}
I do some initialization on preparedStatement on first run of code, and as you can imagine, I have to define isFirst for all queries I use.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这正是静态变量。
您需要使
isFirst
成为MyClass
的(非静态
)成员。并根据您的编辑对其进行重命名:如果您确实“在许多函数中具有此类变量”,那么我建议您重新考虑
MyClass
的设计。鉴于您的示例多么模糊和通用,我无法告诉您如何操作,但您几乎肯定违反了 单一职责原则。That is exactly what a
static
variable is.You need to make
isFirst
a (non-static
) member ofMyClass
. And rename it, following your edit:If you really "have such variables in many functions", then I recommend you rethink the design of
MyClass
. I can't tell you how, given how vague and generic your example is but you're almost certainly violating the Single Responsibility Principle.在您的 MyClass.h 文件中:
在您的构造函数中:
在您的方法中:
您现在可能需要将
isFirst
重命名为mIsFirst
或isFirst_
或无论您的风格指南对成员变量有何建议,因为您现在已将其设为实例成员。您可能还希望在构造函数中使用初始值设定项列表,而不是在构造函数主体中执行此操作。
以上留给读者作为练习。
In your MyClass.h file:
In your constructor:
In your method:
You may want to rename
isFirst
now to something likemIsFirst
orisFirst_
or whatever your style guide recommends for member variables, since you have now made it an instance member.You may also wish to use an initializer list in the constructor, instead of doing it in the body of constructor.
The above left as an exercise for the reader.
在类中使用实例变量。单个类的各个成员函数应该紧密耦合,并且不会破坏彼此的数据。
Use an instance variable in the class. The various member functions of a single class are expected to be tightly coupled, and not stomp on each other's data.