Jump to content
  • Advertisement
Sign in to follow this  
JohnsonGPS

Cannot understand the following codes

This topic is 4443 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I got problems on understanding the attached codes. It seems a 3rd party API library is called. Question1: Why assign NULL to GO_FUNCS? Question2: GO_FUNCS looks like a struture of functions. Is there a term for it? Question 3: I cannot understand this line in total. (*(void (*) (int,t_go_measurements[]))(stGOFuncs.p_go_Measurements))(i_,ast_); Why parenthesize an integer and a structure together? What is possibly the type of stGOFuncs.p_go_Measurements? I did not see its definition. Why put void (*) ahead of the line? I know these questions might seem silly to you, but please help. I also want to get some links for this type of programming. //////////////////////////////////////////////////////////////////////////////////////////////////////////////// #include "goapi.hpp" GO_FUNCS stGOFuncs = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL}; void go_Measurements(int i_, t_go_measurements ast_[]) { (*(void (*) (int,t_go_measurements[]))(stGOFuncs.p_go_Measurements))(i_,ast_); } //Find the definition of t_go_measurements in the header file /* * This structure holds a set of measurements */ typedef struct { long lSpeed; int iUnit; ... double dDistance; } t_go_measurements;

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by JohnsonGPS

I got problems on understanding the attached codes. It seems a 3rd party API library is called.

Question1: Why assign NULL to GO_FUNCS?

Question2: GO_FUNCS looks like a struture of functions. Is there a term for it?

It's an array of function pointers, and, um, it's an array of function pointers.
Quote:

Question 3: I cannot understand this line in total.
(*(void (*) (int,t_go_measurements[]))(stGOFuncs.p_go_Measurements))(i_,ast_);

It's casting something (whatever p_go_Measurements is declared as) into a type of pointer-to-function-taking-int-and-p_go_Measurements[]-and-returning-void, dereferncing it, and invoking whatever function it points to.

This is called "obfuscated C" and was what we wrote before C++ came along if we wanted to do object-style programming from a high-level assembly language.

Share this post


Link to post
Share on other sites

Thank you very much for your reply, Smith or Sam,

Just one more question, you mentioned that this was for object-style programming from a high-level assembly language before C++ comes. Could you please tell me what is the C++ equivalent form of the above codes as well?

Johnson

Share this post


Link to post
Share on other sites
Like..


class ISomething
{
public:
virtual void DoMeasure(int Id, SMeasurements variables[]) = 0;
};

class CMeasureMethod1 : public ISomething
{
public:
void DoMeasure(int Id, SMeasurements variables[])
{
// Some code here
}
};
class CMeasureMethod2 : public ISomething
{
void DoMeasure(int Id, SMeasurements variables[])
{
// Some other code here
}
};

ISomething *MeasureObjects = {new CMeasureMethod1, new CMeasureMethod2, NULL};



Could be used.. There are probably better ways too, but I don't really know what the code's suppoised to do..

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!