Or virtual functions.

Source: Internet
Author: User

For details about the C ++ virtual functions, refer to the blog virtual function table parsing (go to Chen Hao) and share with you some of the problems involved. Let's take a look at a piece of code:

#include <iostream>  using namespace std;    typedef void (*Fun)(void);  class BaseA  {  public:      virtual void A1(){cout<<"A1"<<endl;}      virtual void A2(){cout<<"A2"<<endl;}      virtual void A3(){cout<<"A3"<<endl;}  };  class BaseB  {  public:       virtual void B1(){cout<<"B1"<<endl;}      virtual void B2(){cout<<"B2"<<endl;}      virtual void B3(){cout<<"B3"<<endl;}  };  class BaseC:public BaseA,public BaseB  {  public:      virtual void A1(){cout<<"C.A1"<<endl;}      virtual void A2(){cout<<"C.A2"<<endl;}      virtual void A3(){cout<<"C.A3"<<endl;}       virtual void B1(){cout<<"C.B1"<<endl;}      virtual void B2(){cout<<"C.B2"<<endl;}      virtual void B3(){cout<<"C.B3"<<endl;}  private:      virtual void C1(){cout<<"C1"<<endl;}      virtual void C2(){cout<<"C2"<<endl;}      virtual void C3(){cout<<"C3"<<endl;}  };    int main()  {     Fun fun=NULL;    BaseA* baseA1 = new BaseC;    BaseB* baseB1 = new BaseC;    BaseA* baseA2 = (BaseA*)(void*)baseB1;    BaseB* baseB2 = (BaseB*)(void*)baseA1;        cout<<"baseA1->A1()=";baseA1->A1();    cout<<"baseB1->B1()=";baseB1->B1();    cout<<"baseA2->A1()=";baseA2->A1();    cout<<"baseB2->B1()=";baseB2->B1();    fun= (Fun)*((int*)*(int*)(baseA1)+3);     fun();    //system("pause");      return 0;  }  

 

Here it is necessary to first explain typedef void (Fun *) (void); this part from Baidu know http://zhidao.baidu.com/link? Url = FfhL3LI823olsaln6p-KBKU-BercosGNFxgIpaMNS_ErverXjrenlu1n3NfBd75qfwdw170Aljmk-8YkdaraPa defines a function pointer type. For example, you have three functions: void hello (void) {printf ("hello! ");} Void bye (void) {printf (" goodbye! ");} Void OK (void) {printf (" OK! ");} Typdef void (* funcptr) (void); this constructs a general function. You can use it like this: void speak (int id) {funcptr words [3] = {& hello, & bye, & OK}; funcptr fun = words [id]; (* fun, if speak (0) is displayed, "Hello!" Speak (1) will show "Goodbye !" Speak (2) will display "OK !" Function pointers can be used for a group of functions that process parameters and return values in the same form but with uncertain functions. For example, arithmetic operators, including addition, subtraction, multiplication, and division, can all be represented by typedef int (* calc) (int, int, and so on /************************************** **************************************** **************************************** **************************************** */what will the result display? BaseA1-> A1 () = C. a1baseB1-> B1 () = C. b1baseA2-> A1 () = C. b1baseB2-> B1 () = C. a1C1 why baseA2-> A1 () = C. b1? Why is baseB2-> B1 () = C. A1? I believe that I understand the C ++ virtual function mechanism and understand what is going on. The focus is on fun () and the result is C1! I don't know why I Can See That C1 in the BaseC class is declared in the private domain, but we can access private functions by operating BaseA1, again, we recommend that you read and repost the article to clarify the mechanism of the C ++ virtual function). This shows that C ++ lacks security considerations, at least not rigorous.

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.