xxd
xxd

Reputation: 33

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

Upvotes: 1

Views: 219

Answers (5)

Muhammad Danish
Muhammad Danish

Reputation: 109

int tx(x); explanation.

int x(5); a variable x. and we are initilizing variabe at its creation time.

int x = 5;// in this statement we are assigning 5 to varible x. x in this case already declared. we updating its value just.

Upvotes: 0

phillip voyle
phillip voyle

Reputation: 1993

It's a copy constructor. In c++ the confusion arises when you declare a variable, with no parameters. 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.

Upvotes: 1

Douglas
Douglas

Reputation: 490

It means declare an int type variable named tx. Invoke the constructor tx(x) to initialize tx, its value is x. The code can also written like this:

int tx = x;

Upvotes: 2

Tadeusz Kopec for Ukraine
Tadeusz Kopec for Ukraine

Reputation: 12403

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);

Upvotes: 2

Joop Eggen
Joop Eggen

Reputation: 109547

The same as

int tx = x;

"An int constructor"

Upvotes: 2

Related Questions