Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parenthesis after variable name C++

Tags:

c++

syntax

Working with the below source code (it is open source) and I've never seen parenthesis after a variable name. UDefEnergyH is definitely a variable as can be seen in line 1. Can anyone tell me what these parenthesis are doing? Don't really know how to Google this. Thanks.

bins[0] = UDefEnergyH.GetLowEdgeEnergy(size_t(0));
vals[0] = UDefEnergyH(size_t(0)); //Don't know what this does???
sum = vals[0];
for (ii = 1; ii < maxbin; ii++) {
    bins[ii] = UDefEnergyH.GetLowEdgeEnergy(size_t(ii));
    vals[ii] = UDefEnergyH(size_t(ii)) + vals[ii - 1];
    sum = sum + UDefEnergyH(size_t(ii));
}

And it is declared here in the header file:

G4PhysicsOrderedFreeVector UDefEnergyH;
like image 774
Zach Babbitt Avatar asked Jun 05 '15 07:06

Zach Babbitt


People also ask

What do parentheses do in C?

Using parentheses defensively reduces errors and, if not taken to excess, makes the code more readable. Subclause 6.5 of the C Standard defines the precedence of operation by the order of the subclauses.

Do parentheses work in C?

Parentheses are used in C expressions in the same manner as in algebraic expressions.

What does a * After a variable in C mean?

Here, * is called dereference operator. This defines a pointer; a variable which stores the address of another variable is called a pointer. Pointers are said to point to the variable whose address they store.


2 Answers

It appears operator() is overloaded for the tyupe of UDefEnerfyH.

One way to do this is this solution

#include <iostream>
using namespace std;

struct MJ {
    void GetLowEdgeEnergy(size_t arg) {
        cout << "GetLowEdgeEnergy, arg = " << arg << endl;
    }
    void operator ()(size_t arg) {
        cout << "operator (), arg = " << arg << endl;
    }
};

int main() {
    MJ UDefEnergyH;
    UDefEnergyH.GetLowEdgeEnergy(42);
    UDefEnergyH(42);
    return 0;
}
like image 158
Mohit Jain Avatar answered Oct 02 '22 18:10

Mohit Jain


It seems you are referring to the field in the class G4SPSEneDistribution. Its type is G4PhysicsOrderedFreeVector. And have a look at its members here. As you can see there is operator() overloaded and apparently this is what is called on the second line. It is not very easy to find out what that does, but if you have a look at the comment in the header file for G4PhysicsVector, you will see:

00100          // Returns simply the value in the bin specified by 'binNumber'
00101          // of the dataVector. The boundary check will not be Done. If 
00102          // you want this check, use the operator [].
like image 36
Ivaylo Strandjev Avatar answered Oct 02 '22 19:10

Ivaylo Strandjev