1

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

gawi
  • 2,843
  • 4
  • 29
  • 44
xxd
  • 33
  • 2

5 Answers5

2

The same as

int tx = x;

"An int constructor"

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
2

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;
sje397
  • 41,293
  • 8
  • 87
  • 103
Douglas
  • 490
  • 3
  • 8
  • 1
    Fundamental types don't have constructors. – awesoon Jul 18 '13 at 11:25
  • @soon - I think they can: http://stackoverflow.com/questions/11117450/default-constructor-for-int – sje397 Jul 18 '13 at 11:45
  • @sje397, constructor is a member function. Does `int` have member functions? :) – awesoon Jul 18 '13 at 11:50
  • @soon - big debate here: http://stackoverflow.com/questions/5113365/do-built-in-types-have-default-constructors ... I guess it depends on whether you want to beleive SO or Bjarne himself :) – sje397 Jul 18 '13 at 11:53
  • 1
    @sje397, I of course believe Bjarne and SO, but in most case I believe the Standard. And I can't find any information about the existence of constructors for fundamental types. But you also right - that discussion is big, and I don't think we should debate it here, so, this is my last comment here. Have a nice day! – awesoon Jul 18 '13 at 12:19
2

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);
Tadeusz Kopec for Ukraine
  • 12,283
  • 6
  • 56
  • 83
1

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.

phillip voyle
  • 1,863
  • 2
  • 15
  • 18
0

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.

Muhammad Danish
  • 111
  • 1
  • 6
  • 20