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

  • SEARCH
  • Home
  • 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 6157967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T20:52:28+00:00 2026-05-23T20:52:28+00:00

I discovered the source of a really insidious bug today. It’s all safely fixed

  • 0

I discovered the source of a really insidious bug today. It’s all safely fixed now, but I’d like to understand why when I execute the following code:

using namespace System;

ref class EvilClass
{
public:
    EvilClass()
    {

    }

    void Print()
    {
        static bool enablePrint = false;
        if( enablePrint )
        {
            Console::WriteLine("PrintEnabled");
        }
        else
        {
            Console::WriteLine("PrintDisabled");
        }
        enablePrint = true;
    }

};


int main(array<System::String ^> ^args)
{
    Console::WriteLine(L"Hello World");

    EvilClass^ ec = gcnew EvilClass();

    ec->Print();
    ec->Print();

    delete ec;
    ec = nullptr;

    ec = gcnew EvilClass();

    ec->Print();
    ec->Print();

    delete ec;
    ec = nullptr;

    return 0;
}

…I get the following:

Hello World
PrintDisabled
PrintEnabled
PrintEnabled
PrintEnabled

I had always assumed that the static would only persist between calls to the same instance of a class?

  • 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-23T20:52:28+00:00Added an answer on May 23, 2026 at 8:52 pm

    Your assumption is incorrect. A function-scoped static variable is very similar to a global variable. There is only one global instance of it. More details here: What is the lifetime of a static variable in a C++ function?

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

Sidebar

Related Questions

I recently discovered a bug in NHibernate project & reported & fixed the source
I discovered that the source of a bug in some code I was writing
So I recently discovered this Source Mate tool for Flash builder with all the
I discovered raven-db and I liked it but then I saw the license... GPL
Possible Duplicate: C++ source in unicode I just discovered this line of code in
I just discovered a weird bug in my Delphi Application. When I run the
I just discovered a bizarre bug in my program related to its use of
A friend's server (yes, really. Not mine.) was broken into and we discovered a
I was looking at open source ALM tools and discovered an interesting project called
I recently discovered the existence of source maps in chrome via source debugging in

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.