Dynamic binding and polymorphism of virtual functions in C + +

Source: Internet
Author: User

Directory

      • Static type vs dynamic type, static binding vs Dynamic binding two sets of concepts
      • Implementation mechanism of virtual function
      • Polymorphism
I. Static VS dynamic

  static type VS dynamic type . A static type refers to the type of the object declaration, as determined by the compiler. A dynamic type refers to the type that the object is pointing to, the dynamic type can be changed, and the static type cannot be changed. An inheritance relationship causes the object's pointers and references to have static and dynamic types, because an upward-down type conversion between types may exist in the existence of an inheritance relationship. static binding VS dynamic binding . An attribute (function) relies on the static type of an object, determined at compile time, that an attribute (function) depends on the dynamic type of the object and is determined at run time . Dynamic binding can occur only when a virtual function is called by a reference to a base class, or if the object is used to manipulate virtual functions, static binding is still used. Because a reference or pointer can either point to a base class object or to a base class part of a derived class object, a virtual function that is called with a reference or a pointer . For example, in the following example, the static type of PD is D*,PB static type is b*, is determined by the compiler, and the dynamic type of PD and PB are d*. DoSomething () A normal member function is a method that is statically bound at the compiler, invoking its own static type. Vfun () is a virtual member function that is dynamically bound and therefore calls a method of the dynamic type, that is, the vfun in the D type.

1#include <iostream>2 using namespacestd;3 4 classB5 {6  Public:7     voiddosomething () {8Std::cout <<"B::D osomething ()"<<Endl;9     }Ten     Virtual voidVfun () { OneStd::cout <<"B::vfun ()"<<Endl; A     } - }; -  the classB | PublicB - { -  Public: -     voiddosomething () { +Std::cout <<"D::D osomething ()"<<Endl; -     } +     Virtual voidVfun () { AStd::cout <<"D::vfun ()"<<Endl; at  -     } - }; -  - intMain () - { ind* PD =NewD (); Pointer presence Note static type and dynamic type -b* PB =PD; Note the static type and dynamic type toPd->dosomething (); Ordinary member functions, statically bound methods in static types +Pb->dosomething (); Ordinary member functions, statically bound methods in static types - thePd->Vfun (); Virtual member function, dynamically bound to a method in a dynamic type *Pb->Vfun (); Virtual member function, dynamically bound to a method in a dynamic type $     return 0;Panax Notoginseng}
Two. The implementation mechanism of virtual function

Second, C + + polymorphism

1. Polymorphism definition: The same operation acts on different objects, can have different interpretations, that is, to produce different execution effects . There are two kinds of polymorphism, namely, the multi-state at compile time, that is function overloading, and runtime polymorphism, which is realized by virtual members when the system is running. The latter is generally referred to

2. Typical Use Cases

1#include <iostream>2 using namespacestd;3 4 class Person5 {6  Public:7     Virtual voidprint () {8Std::cout <<"I ' m a person"<<Endl;9     }Ten     One }; A  - classChinese: Public Person - { the  Public: -     Virtual voidprint () { -Std::cout <<"I ' m as Chinese"<<Endl; -     } +     - }; +  A classAmerican: Public Person at { -  Public: -     Virtual voidprint () { -Std::cout <<"I ' m as American"<<Endl; -     } -     in }; -  to  + //Reference - voidPrintperson (person&Person ) { the person.print (); * } $ //PointerPanax Notoginseng voidPrintperson (person*p) { -P->print (); the } + intMain () A { the Person p; + Chinese C; - American A; $ Printperson (p); $ Printperson (c); - Printperson (a); -  thePrintperson (&p); -Printperson (&c);WuyiPrintperson (&a); the      -     return 0; Wu}
View Code

4. Polymorphic implementations: Polymorphism is implemented by inheritance and virtual functions, because virtual functions are dynamically bound and depend on the dynamic type of the calling object (static type is a pointer and reference to the base class), so depending on the dynamic type, the operation is different, that is, polymorphism.

3. Virtual function implementation: Simple to say virtual function is through the virtual function table to achieve.

Each class with a virtual function will have a virtual function table (VTBL), and each entry in the table records its address of a virtual function. Actually an array of function pointers. Stores the address of the virtual function table at the front of the object of the class.

Virtual function tables are also inherited and overridden in the class's inheritance, and when there is a rewrite, polymorphism occurs.

1#include <iostream>2 using namespacestd;3 class Person4 {5  Public:6     Virtual voidprint () {7Std::cout <<"I ' m a person"<<Endl;8     }9     Virtual voidfoo () {}   One }; A  - classChinese: Public Person - { the  Public: -     Virtual voidprint () { -Std::cout <<"I ' m as Chinese"<<Endl; -     } +     Virtual voidFoo2 () {} - }; + classAmerican: Public Person A { at  Public: -     Virtual voidprint () { -Std::cout <<"I ' m as American"<<Endl; -     }   - }; - //Reference in voidPrintperson (person&Person ) { - person.print (); to } + //Pointer - voidPrintperson (person*p) { theP->print (); * } $ intMain ()Panax Notoginseng { - Person p; the Chinese C; + American A; APrintperson (&p); thePrintperson (&c); +Printperson (&a);  -     return 0; $}

Vtbl:person of the person class: Address of the:p rint (), Person::foo ()

Address of the Chinese class: Chinese: The address of the:p rint (), the address of the Person::foo (), the address of the PERSON::FOO1 ()

Address of the American class: American: The address of the:p rint (), the address of the Person::foo ()

Iii. Summary

Polymorphism is one of the three major features of C + +, it is very important that the conditions are inherited, a virtual member function exists in the base class, a virtual member function of the derived class override (overriding) The base class, a pointer to a base class object in code, or a reference call to a virtual member function. The result of the run behaves as a function that actually calls the respective derived class overrides.

A polymorphic core virtual member function is dynamically bound, that is, dependent on the dynamic type of the object. The difficulty of understanding polymorphism is the implementation mechanism of virtual function, that is, virtual function table.

Reference: http://blog.csdn.net/chgaowei/article/details/6427731

Dynamic binding and polymorphism of virtual functions in C + +

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.