An apple company (China) Interview Topic

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.