简体   繁体   English

这是函数调用还是变量声明?

[英]Is this a function call or a variable declaration?

I can not understand the meaning of the following code, please help me, thank you. 我无法理解以下代码的含义,请帮助我,谢谢。

In the following code: 在下面的代码中:

FrameDetect::Point FrameDetect::tracer(LabelData *ldata, int x, int y, int &pos, int lbl)
{
    for (int i=7; i>=0; i--)
    {
        int tx(x);
        int ty(y);
        nextPoint(tx, ty, pos);
        if (tx>0 && ty>0 && tx < bimg->width() && ty < bimg->height())
        {
            const int &l( ldata->at(tx, ty) );
            if (bimg->at(tx, ty) == ccolor && (l == 0 || l == lbl))
            {
                return Point(tx, ty);
            }
            if (bimg->at(tx, ty) == bcolor)
            {
                ldata->at(tx, ty) = -1;
            }
        }
        pos = (pos + 1)%8;
    }
    return Point(-1, -1);
}

int tx(x); is function call or variable declaration? 是函数调用还是变量声明? Thanks for your help. 谢谢你的帮助。

Source 资源

The same as 与...相同

int tx = x;

"An int constructor" “一个int构造函数”

It means declare an int type variable named tx . 这意味着声明一个名为txint类型变量。 Invoke the constructor tx(x) to initialize tx , its value is x . 调用构造函数tx(x)初始化tx ,其值为x The code can also written like this: 该代码也可以这样写:

int tx = x;

It is a variable declaration. 这是一个变量声明。 It can't be parsed as a function declaration, because an expression in parenthesis does not name a type. 不能将其解析为函数声明,因为括号中的表达式未命名类型。
It can't be a function call either - the syntax is invalid. 也不能是函数调用-语法无效。 You can't write 你不会写

double sin(2);

It's a copy constructor. 这是一个复制构造函数。 In c++ the confusion arises when you declare a variable, with no parameters. 在c ++中,当您声明不带参数的变量时,会引起混淆。 In that situation you omit the brackets 在这种情况下,您可以省略括号

I'll present several examples: 我将举几个例子:

void afunction_thatDoesNothing(int x)
{
   int aFuncDecl();     //1: function declaration
   int aVariable;       //2: default construction of int
   int aValue1 = x;     //3: constructing with x
   int aValue2(x);      //4: constructing with x
   int aFuncDecl2(int); //5: declaration of a function taking an int
}

The only case above where there is a declaration vs initialization ambiguity is case 1 - in your code you've supplied a value typed expression to the constructor (case 4), and it can not be misinterpreted as a declaration. 上面唯一有声明与初始化模糊性的情况是情况1-在代码中,您已向构造函数提供了一个值类型表达式(情况4),并且不能将其误解为声明。

int tx(x); int tx(x); explanation. 说明。

int x(5); int x(5); a variable x. 变量x。 and we are initilizing variabe at its creation time. 并且我们正在创建可变参数时对其进行初始化。

int x = 5;// in this statement we are assigning 5 to varible x. int x = 5; //在此语句中,我们将5赋给变量x。 x in this case already declared. 在这种情况下,x已经声明。 we updating its value just. 我们只是更新其价值。

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

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