在实例之间分离类成员函数内的静态变量

发布于 2024-12-10 04:44:46 字数 1027 浏览 0 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

恬淡成诗 2024-12-17 04:44:46

问题是 MyClass 的所有实例似乎都共享静态变量。

这正是静态变量。

如何使变量不被不同实例共享(但在同一实例之间共享)?

您需要使 isFirst 成为 MyClass 的(非静态)成员。并根据您的编辑对其进行重命名:

class MyClass
{
public:
    MyClass();
    void Foo();
    void Bar();
private:
    bool should_foo;
    bool should_bar;
};

MyClass::MyClass()
    :should_foo(true)
    ,should_bar(true)
{
}

void MyClass::Foo()
{
    if (should_foo)
    {
        // do something;
        should_foo = false;
    }
}

void MyClass::Bar()
{
    if (should_bar)
    {
        // do something;
        should_bar = false;
    }
}

如果您确实“在许多函数中具有此类变量”,那么我建议您重新考虑MyClass的设计。鉴于您的示例多么模糊和通用,我无法告诉您如何操作,但您几乎肯定违反了 单一职责原则

The problem is all instances of MyClass seem to share the static variable.

That is exactly what a static variable is.

How can I make the variable not-shared by different instances(but shared among same instance)?

You need to make isFirst a (non-static) member of MyClass. And rename it, following your edit:

class MyClass
{
public:
    MyClass();
    void Foo();
    void Bar();
private:
    bool should_foo;
    bool should_bar;
};

MyClass::MyClass()
    :should_foo(true)
    ,should_bar(true)
{
}

void MyClass::Foo()
{
    if (should_foo)
    {
        // do something;
        should_foo = false;
    }
}

void MyClass::Bar()
{
    if (should_bar)
    {
        // do something;
        should_bar = false;
    }
}

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.

老旧海报 2024-12-17 04:44:46

在您的 MyClass.h 文件中:

class MyClass {
public:
  MyClass();
  void Foo();

private:
  bool isFirst;
}

在您的构造函数中:

MyClass::MyClass() {
  isFirst = true;
}

在您的方法中:

void MyClass::Foo()
{
  if (isFirst)
    do something;
  isFirst = false;
}

您现在可能需要将 isFirst 重命名为 mIsFirstisFirst_ 或无论您的风格指南对成员变量有何建议,因为您现在已将其设为实例成员。

您可能还希望在构造函数中使用初始值设定项列表,而不是在构造函数主体中执行此操作。

以上留给读者作为练习。

In your MyClass.h file:

class MyClass {
public:
  MyClass();
  void Foo();

private:
  bool isFirst;
}

In your constructor:

MyClass::MyClass() {
  isFirst = true;
}

In your method:

void MyClass::Foo()
{
  if (isFirst)
    do something;
  isFirst = false;
}

You may want to rename isFirst now to something like mIsFirst or isFirst_ 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.

灯下孤影 2024-12-17 04:44:46

在类中使用实例变量。单个类的各个成员函数应该紧密耦合,并且不会破坏彼此的数据。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文