简体   繁体   English

通过else定义函数中的变量

[英]Defining variable in function through if else

Please see the small code below: 请查看下面的小代码:

void TestPluginAPI::MouseMove(int nX, int nY)
{
    INPUT input;
    DOUBLE fScreenWidth = GetSystemMetrics( SM_CXSCREEN )-1; 
    DOUBLE fScreenHeight  = GetSystemMetrics( SM_CYSCREEN )-1; 
    int plusY;
    if (FullScreenCheck() == 1) {int plusY = getmidY() + 8.0f;}
    else {int plusY = getmidY();}
    int plusX = getmidX();
    DOUBLE fX = plusX*(65535.0f/fScreenWidth) + (nX*(65535.0f/fScreenWidth));
    DOUBLE fY = plusY*(65535.0f/fScreenHeight) + (nY*(65535.0f/fScreenHeight));

    RtlZeroMemory(&input, sizeof(input));
    input.type = INPUT_MOUSE;
    input.mi.dwFlags = MOUSEEVENTF_MOVE|MOUSEEVENTF_ABSOLUTE;
    input.mi.dx = (LONG)fX;
    input.mi.dy = (LONG)fY;

    SendInput(1, &input, sizeof(input));
}

The thing is that the compiler gives me a warning, that plusY is undefined, and obviously if I try to run this function in a compiled plugin (it's from plugin code, debugging it thru javascript console in a browser) it crashes because plusY is undefined. 关键是编译器会警告我, plusY是未定义的,并且很显然,如果我尝试在已编译的插件中运行此函数(来自插件代码,通过浏览器中的javascript控制台对其进行调试),则会崩溃,因为plusY是未定义的。

It seems that if I can't define variables in function through if-else, then how can I do this? 看来,如果我无法通过if-else在函数中定义变量,那我该怎么办呢?

{int plusY = getmidY() + 8.0f;}

int plusY stops existing at the } in both if statements int plusY在两个if语句中的} int plusY停止存在

int plusY inside brackets {} is a different variable than int plusY outside. int plusY内部括号{}是比一个不同的变量int plusY外部。

You should do this 你应该做这个

int plusY;
if (FullScreenCheck() == 1) {plusY = getmidY() + 8.0f;}
else {plusY = getmidY();}

Variables defined in one of the branches of an if have the scope of that branch only. if分支之一中定义的变量仅具有该分支的范围。 Even if you eliminated the braces (which would be legal here): 即使您消除了花括号(在这里也是合法的):

if ( fullScreenCheck() == 1 )
    int plusY = getmidY() + 8.0f;
else
    int plusY = getmidY();

will result in a plusY whose scope ends after the if . 将产生一个plusY其作用域在if之后结束。

The best way of handling this is: 最好的处理方法是:

int plusY = fullScreenCheck() == 1
            ? getmidY() + 8.0f
            : getmidY();

Because of the restrictions on what you can do in a single expression, it's not always possible to do it, but whenever you can, it results in far more readable code. 由于在单个表达式中可以执行的操作受到限制,因此并非总是可以做到这一点,但是只要有可能,它就会导致可读性更高的代码。

plusY is destructed from memory at the closing brace of the if statement. 在if语句的结尾处, plusY从内存中破坏了。 If you need to use its after the if statement then you should declare it outside the block: 如果需要在if语句之后使用它,则应在块声明它:

int plusY;

if (...)
{
    plusY = getmidY() + 8.0f;
}
int plusY;

You have declared a variable named plusY which is available to your entire function 您已经声明了一个名为plusY的变量,该变量可用于整个函数

if (FullScreenCheck() == 1) {int plusY = getmidY() + 8.0f;}

This declares another variable which also happens to be named plusY , but this new variable is only available inside of the if statement. 这将声明另一个变量,该变量也恰好名为plusY ,但是此新变量仅在if语句内部可用。

else {int plusY = getmidY();}

And now you declare a third variable with the same name which is only available in the else clause. 现在,您声明具有相同名称的第三个变量,该变量仅在else子句中可用。

To fix this and use the same variable in all locations, change your code to 要解决此问题并在所有位置使用相同的变量,请将代码更改为

int plusY;
if (FullScreenCheck() == 1) {plusY = getmidY() + 8.0f;}
else {plusY = getmidY();}

ps As an aside, you should learn about typical code formatting practices and conventions. ps另外,您应该了解典型的代码格式化实践和约定。

pps You should also initialize plusY to a value which is otherwise invalid so you can easily track down any problems. pps您还应该将plusY初始化为一个否则无效的值,以便您可以轻松地plusY任何问题。 Alternatively, you can use the ternary operator in order to avoid this issue entirely. 或者,可以使用三元运算符来完全避免此问题。 In this situation, the ternary operator is most likely the best solution. 在这种情况下,三元运算符很可能是最佳解决方案。

您可以将整个if / else替换为这一行(固定了作用域规则)。

int plusY = (FullScreenCheck() == 1) ? getmidY() + 8.0f : getmidY();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM