c++ - How does this particular struct function like this? (arcsynthesis modern 3D graphics programming) -


i using arcsynthesis' tutorial learn modern 3d graphics programming, , while understanding of have run snag author calls "complex management code" can't understand how code works:

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;     } };   instance g_instancelist[] = {     {stationaryoffset},     {ovaloffset},     {bottomcircleoffset}, }; 

i don't quite how function pointer getting set inside struct in g_instancelist[] don't syntactically how works , how g_instancelist used in next segment here:

float felapsedtime = glutget(glut_elapsed_time) / 1000.0f;     for(int iloop = 0; iloop < array_count(g_instancelist); iloop++)     {         instance &currinst = g_instancelist[iloop];         const glm::mat4 &transformmatrix = currinst.constructmatrix(felapsedtime);          gluniformmatrix4fv(modeltocameramatrixunif, 1, gl_false,     glm::value_ptr(transformmatrix));     gldrawelements(gl_triangles, array_count(indexdata), gl_unsigned_short, 0); } 

i thought pretty familiar c++ point syntactic sugar new me. in advance :)

i don't quite how function pointer getting set inside struct in g_instancelist[]

instance an aggregate type. may have member function, doesn't make not aggregate. in c++98/03, objects of aggregate types can initialized aggregate initialization:

struct aggregate {   int x;   int y; };  aggregate agg = { 5, 4 }; assert(4 == agg.y); //will true. 

each member in struct initialized each individual value, in order in members declared in struct. 5 goes x , 4 goes y.

instance aggregate. , has 1 member. member happens function pointer type, c++ doesn't care; it's value , can used in aggregate initialization.

a c++ array aggregate well. can initialize array aggregate initialization:

int arr[4] = {3, 45, 9, 81}; 

c++ lets initialize array aggregate initialization, size of array determined initializer:

int arr[] = {5, 2}; //arr have 2 elements. 

therefore, g_instancelist array, , therefore aggregate subject aggregate initialization. has size determined number of values provided aggregate initialization syntax. each element of array also aggregate, each element of array can therefore aggregate initialized.

instance g_instancelist[] = {     {stationaryoffset},     {ovaloffset},     {bottomcircleoffset}, }; 

each member of aggregate initialization list aggregate initialization initializes instance object. stationaryoffset , rest function pointers match signature of member of instance. that's how instance::calcoffset gets filled in.


how g_instancelist used in next segment here

how gets used pretty common c++. first line gets reference 1 of instance array elements. done make code shorter; doesn't have use g_instancelist[iloop] every time talk instance it's using. second line calls member function of instance (which calls internal function pointer), storing result in variable.

if you're talking array_count, that's convenient macro get... well, count of array elements. c++ compiler knows how big g_instancelist is, macro used make compiler compute itself. way, if g_instancelist array length changes, don't have go through entire codebase find every use of array's length. also, never have directly state length; can infer through aggregate initialization.


Comments

Popular posts from this blog

plot - Remove Objects from Legend When You Have Also Used Fit, Matlab -

java - Why does my date parsing return a weird date? -

Need help in packaging app using TideSDK on Windows -