Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6698999
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T06:38:37+00:00 2026-05-26T06:38:37+00:00

MyClass::Foo() { static bool isFirst = true; if (isFirst) do something; isFirst = false;

  • 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;
}

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T06:38:37+00:00Added an answer on May 26, 2026 at 6:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class like: class MyClass: Foo = 1 Bar = 2 Whenever
I have a C++ method such as: bool MyClass::Foo(char* charPointer) { return CallExternalAPIFunction(charPointer); }
What's the best way to abstract this pattern: class MyClass attr_accessor :foo, :bar def
I have a class: class MyClass: def __init__(self, foo): if foo != 1: raise
Say I have: @property (nonatomic,retain) NSString *foo; in some class. And I call: myclass.foo
I have a class that is something like the following: public class Foo {
Should be easy... class Base{} class Foo:Base{} public bool Bar(Type t){ // return ???
If you have a method such as this: float method(myClass foo) { return foo.PrivateVar()
function MyClass(projectName) { this.projectName = projectName; } MyClass.prototype.createHttpRequestObject = function() { /* do something
class MyClass { public: void method2() { static int i; ... } }; Will

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.