变量名后的括号 C++
Parenthesis after variable name C++
使用以下源代码(它是开源代码),我从未在变量名后看到括号。 UDefEnergyH 绝对是一个变量,如第 1 行所示。谁能告诉我这些括号在做什么?真的不知道如何 Google 这个。谢谢
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));
}
并在头文件中声明:
G4PhysicsOrderedFreeVector UDefEnergyH;
operator()
似乎对 UDefEnerfyH
的类型进行了重载。
一种方法是 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;
}
这就是所谓的直接初始化,它首先构造一个以'0'为直接参数的对象,然后将其赋值给vals数组的第一个索引。
您似乎指的是 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 中的字段,您将看到:
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 [].
使用以下源代码(它是开源代码),我从未在变量名后看到括号。 UDefEnergyH 绝对是一个变量,如第 1 行所示。谁能告诉我这些括号在做什么?真的不知道如何 Google 这个。谢谢
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));
}
并在头文件中声明:
G4PhysicsOrderedFreeVector UDefEnergyH;
operator()
似乎对 UDefEnerfyH
的类型进行了重载。
一种方法是 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;
}
这就是所谓的直接初始化,它首先构造一个以'0'为直接参数的对象,然后将其赋值给vals数组的第一个索引。
您似乎指的是 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 中的字段,您将看到:
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 [].