C + + multi-state plane question

Source: Internet
Author: User


Face Test1: Please talk about your understanding of polymorphism

Multi-State implementation effect

When a parent object is directed to a subclass object, the function travels back and forth in the subclass to perform a different pattern.

Polymorphism: The same invocation statement has many different forms of expression;


three conditions for polymorphic implementations

There are inheritance, virtual overrides, and a parent pointer (reference) to the child class object.


polymorphic C + + Implement

The virtual keyword tells the compiler that this function needs to support polymorphism, not how to invoke it based on the pointer type, but how to invoke the actual object type that the pointer points to.


The theoretical basis of polymorphism

Dynamic linking PK Static-linked series. The invocation of an overriding function is judged based on the actual object type.

Dynamic linking that determines whether a function of the parent class or a subclass is executed at run time

Static-linked, C + + compile time has determined the function to execute.

the significance of polymorphism

Fundamentals of Design Patterns is the cornerstone of the framework.


theoretical basis for the realization of polymorphism

function pointers do function arguments

C- function pointers are the crowning glory of C + +. There are two common uses of C function pointers (positive, reverse).


A study of polymorphic principle

Vptr Distribution Initialization



Face Test 2 : Talk C + + how the compiler implements Polymorphic                 

1. When a virtual function is declared in a class, the compiler generates a virtual function in the class.

2. A virtual function table is a data structure that stores a pointer to a class member function.

3. Virtual function tables are automatically generated and maintained by the compiler.

The 4.virtual member function is placed into the virtual function table by the compiler.

5. When a virtual function exists, each object has a pointer to the virtual function table (vptr pointer).

650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M01/83/C9/wKiom1d8euejlXh2AACaDYcHaxs877.png "title=" Untitled. png "alt=" Wkiom1d8euejlxh2aacadychaxs877.png "/>



650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/83/C8/wKioL1d8ewHyZ_yTAADk03cU3L4391.png "title=" Untitled. png "alt=" Wkiol1d8ewhyz_ytaadk03cu3l4391.png "/>


650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/C8/wKioL1d8exmCF4bmAABKdjS-4fo791.png "title=" Untitled. png "alt=" Wkiol1d8exmcf4bmaabkdjs-4fo791.png "/>

Illustration 1:

Calling an overriding function through the virtual function table pointer vptr is done when the program runs, so you need to address it to determine which function is really supposed to be called. The normal member function is a function that determines the call at compile time. In efficiency, the efficiency of virtual function is much lower.

Illustration 2:

For efficiency reasons, it is not necessary to declare all member functions as virtual functions



Face Test3: Talk to you about rewriting, overloading understanding

650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/83/C8/wKioL1d8e5nSjEkXAAHa0-vfdIM402.png "title=" Untitled. png "alt=" Wkiol1d8e5nsjekxaaha0-vfdim402.png "/>


Face Test4: Whether each member function of a class can be declared as a virtual function, why.                Face Test5: Is it possible to invoke a virtual function in a constructor to achieve polymorphism? Why?                         
Face Test6: Virtual function table pointer (VPTRHow do you understand the process of being initialized by the compiler?    Face Test7: Is it possible to invoke a virtual function in the constructor of the parent class to get polymorphic? Face Test8: Why do you define a virtual destructor?










This article from "Soul Bucket Luo" blog, declined reprint!

C + + multi-state plane question

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.