I found the following C++ code with syntax I have never before seen. Does anybody care to elaborate how this workss?
Function 1
glm::vec3 BottomCircleOffset(float fElapsedTime)
{
return glm::vec3(.0f,.0f,.0f);
}
Function 2
glm::vec3 OvalOffset(float fElapsedTime)
{
return glm::vec3(.1f, .1f, .1f);
}
Instance Struct
struct Instance
{
typedef glm::vec3(*OffsetFunc)(float);
OffsetFunc CalcOffset;
glm::mat4 ConstructMatrix(float fElapsedTime)
{
glm::mat4 theMat(1.0f);
theMat[3] = glm::vec4(CalcOffset(fElapsedTime), 1.0f);
return theMat;
}
};
Until now this is all fine. I understand OffsetFunc is a typedef for a function pointer taking a float as argument and returning a glm::vec3. I also understand CalcOffset is a variable to such a function.
The code then goes to create an array of Instance types like so:
Instance g_instanceList[] =
{
{StationaryOffset},
{OvalOffset},
{BottomCircleOffset},
};
This is syntax I have never come across before:
- How are we initialising an Instance type by simply putting the name of a function?
- The Instance struct not even having a constructor which takes a function pointer, how does it know to initialise CalcOffset to this value?