Apple has a corporate presence in China (Shanghai), but it feels like it is primarily a testing department, primarily to protect Apple's product quality QE. Interview, the interviewer out of a topic, I seem to have opened a similar topic, but because the spot played poorly did not answer. The main problem is this:
In one of the company's communication protocols, there is a scenario in which a data frame is sent to B, and the first byte of the data frame represents the type of the data frame (four total), which can only be 0,1,2,3. A each time a packet is sent to B, b to write a program to call the corresponding data frame type function to process the data frame. The processing function must correspond to the frame, otherwise it will not get the true result. If...else...,switch and three-mesh expression statements are not required.
The topic here, presumably everybody already knew can use C + + 's virtual function to do, but exactly how should write? Here is an implementation method.
#include <iostream>using namespaceStd;typedefvoid(*pfunc) (void);classsolution{ Public: Virtual voidFunc1 () {cout <<"func1 executed ..."<<Endl;} Virtual voidFunc2 () {cout <<"Func2 executed ..."<<Endl;} Virtual voidFunc3 () {cout <<"func3 executed ..."<<Endl;} Virtual voidFunc4 () {cout <<"Func4 executed ..."<<Endl;} voidDoSomething (intframe_id) { if(frame_id >=0&& frame_id <=3) {PFunc pf; PF= (pFunc) * ((int*)*(int*) This+frame_id); PF (); } }};intMain () {solution sln; Sln.dosomething (0); Sln.dosomething (1); Sln.dosomething (2); Sln.dosomething (3); return 0;}
The results of the implementation are as follows:
Here are some ways to get pointers to virtual functions, see the article: http://www.cppblog.com/xczhang/archive/2008/01/20/41508.html
An apple company (China) Interview Topic