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.
我如何使各种情况没有分担的变数(但在同一例子中分享)?
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.
我的实际守则就是这样。
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);
...
}
我对第一版的准备声明做了一些初步介绍,正如你可以想象的那样,我必须首先确定我使用的所有询问。