开发者

Visual Studio Compiler is highlighting Static variables differently?

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(开发者_如何学C"../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!


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.


Nah, highlighting doesn't mean anything. That is, the editor doesn't call the compiler before deciding how/what/when to highlight. So that is not your problem. Sorry 'bout that :-)

You can prove this to yourself by going to Tools->Options->TextEditor and noticing that you can change the highlighting by choosing a different text-editing model.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜