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 4251506
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T04:40:15+00:00 2026-05-21T04:40:15+00:00

I’m programming in C++ and have a method which uses a static variable. The

  • 0

I’m programming in C++ and have a method which uses a static variable. The method isn’t working as I think it should; upon investigation, I found that my static variable is being highlighted in red in two places and blue in other places. Below is the code:

int GameModeState::changeJob(int number)
{
    static int job = 1; //red
    if (number == 1)
    {
        job = (job+1); //First one is red, second one is blue
        return job; //blue
    } else {
        return job; //blue
    }
}

I’m calling this method with other methods, one shown for example:

int GameModeState::getJob()
{
    int currentJob = (changeJob(2));
    return currentJob;
}

I want a method like getJob() to simply return the current value of job, while another method, when calling changeJob(number) is changeJob(1), to increment job’s value by one. (Hence the if/else statement in changeJob(number)).
Since the job variables are highlighted differently, I’m thinking the compiler is saying that it views the two separately somehow? I’m getting stuck with job being some even value.

EDIT I also have Awesomium… I believe that is the only addition to the compiler, but I’m not completely sure.

MOAR EDIT In another class, I have a method which should determine the current job’s number and do something based on if the number is even or odd (since right now there are only two jobs)

void ZoneMovementState::_changeZone(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
    //Awesomium::JSValue::Object object = input.getObject();
    //String zoneFilename = Convert::toString(object[L"zoneFilename"].toString());

    // If the number from getJob is even, the player is currently a geologist
    if (GameModeState::getJob()%2 == 0)
    {
        ZoneParser::getSingleton().load("../media/zones/geology_zone.xml", false);
    } else {
        ZoneParser::getSingleton().load("../media/zones/farm_zone.xml", false);
    }
    transitionHandler->go();
}

Ignore the two commented out lines; they deal with JS, which I’m not working on for now.
In the program, I can access the farm_zone until I increment job’s value using the below method in GameModeState:

    void GameModeState::_openNotebook(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
    mNotebookTransition->go();
    static int currentJob = changeJob(1);
}

…. So I figured out my problem. While going through the code to show you guys, I realized that the static for currentJob was probably unneeded… once I removed it, my code works as it should now.

Thanks for the help guys!

  • 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-21T04:40:15+00:00Added an answer on May 21, 2026 at 4:40 am

    Part of the problem here is you’re using a static local for what very likely should just be a member variable. A static local maintains it’s value across all calls to a function in all threads in a process. It’s much more likely that you want it to persist for all calls to changeJob in a particular GameModeState instance (else why make it a member functon to begin with?).

    To do this you’ll need to define a member variable on GameModeState initialize it in the constructor and then access it in the method. For example

    class GameModeState {
      int job;
      GameModeState() : job(1) {} 
      int changeJob(int number);
    };
    
    int GameModeState::changeJob(int number) {
        if (number == 1) {
            job = (job+1);
            return job;
        } else {
            return job;
        }
    }
    

    Note: I’m not entirely sure why you’re seeing the color’s your are seeing. Visual Studio by default won’t color member variables a particular color in C++ so it’s very likely another add-in you are using.

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

Sidebar

Related Questions

No related questions found

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.