English 中文(简体)
职类成员内部单独固定变量
原标题:Separate static variable inside class member function among instances
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);
...
}

我对第一版的准备声明做了一些初步介绍,正如你可以想象的那样,我必须首先确定我使用的所有询问。

最佳回答

问题都是我的地图集似乎与静态变数相同的例子。

这正是<代码>static变量。

我如何使各种情况没有分担的变数(但在同一例子中分享)?

您需要将<代码>isFirst a (non-static) 加入MyClass。 页: 1

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.

问题回答

在你本人档案中:

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

private:
  bool isFirst;
}

贵方:

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

您的方法是:

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

您不妨将<代码>isFirst 现在改为mIsFirstisFirst_,或您的风格指南建议的成员变量,因为您现在将这一条作为分析成员。

您也不妨在建筑商中使用初步设计清单,而不是在建筑商的体内这样做。

以上为读者所用。

在该类别中采用实例变量。 单一类别成员的各种职能预计会紧密结合,而不是相互兼容。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签