Visual Studio 编译器以不同方式突出显示静态变量?
我正在使用 C++ 进行编程,并且有一个使用静态变量的方法。该方法没有像我想象的那样起作用;经过调查,我发现我的静态变量在两个地方以红色突出显示,在其他地方以蓝色突出显示。下面是代码:
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
}
}
我使用其他方法调用此方法,例如:
int GameModeState::getJob()
{
int currentJob = (changeJob(2));
return currentJob;
}
我想要一个像 getJob()
这样的方法来简单地返回 job
的当前值,而另一种方法,当调用changeJob(number)时,是changeJob(1)
,将job的值加一。 (因此 changeJob(number)
中的 if/else 语句)。 由于 job
变量的突出显示方式不同,我认为编译器是说它以某种方式分别查看这两个变量?我一直坚持 job
具有某种偶数价值。
编辑 我也有 Awesomium...我相信这是编译器的唯一补充,但我不完全确定。
MOAR 编辑 在另一堂课中,我有一个方法应该确定当前作业的编号,并根据该编号是偶数还是奇数执行某些操作(因为现在只有两个作业)
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();
}
忽略两个评论出线;他们与 JS 打交道,我现在不做这方面的工作。 在程序中,我可以访问farm_zone
,直到我使用GameModeState
中的以下方法增加作业的值:
void GameModeState::_openNotebook(const String& message, const Awesomium::JSValue& input, Awesomium::JSValue& output)
{
mNotebookTransition->go();
static int currentJob = changeJob(1);
}
...所以我找到了我的问题。在通过代码向大家展示时,我意识到 currentJob
的静态可能是不需要的......一旦我删除它,我的代码就可以正常工作了。
感谢各位的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这里的部分问题是您正在使用静态局部变量,而很可能应该只是一个成员变量。静态局部在进程中所有线程中对函数的所有调用中维护其值。您更有可能希望它在特定
GameModeState
实例中对changeJob
的所有调用中持续存在(否则为什么要让它成为一个成员函数呢?)。为此,您需要在
GameModeState
上定义一个成员变量,在构造函数中对其进行初始化,然后在方法中访问它。例如注意:我不完全确定为什么你会看到你所看到的颜色。默认情况下,Visual Studio 不会将 C++ 中的成员变量着色为特定颜色,因此它很可能是您正在使用的另一个加载项。
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 particularGameModeState
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 exampleNote: 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.