C + + multi-state implementation principle

Source: Internet
Author: User

C + + polymorphism is summed up in a nutshell: By adding the virtual keyword to a function in the base class, overriding the function in a derived class, the runtime will invoke the corresponding function based on the actual type of the object. If the object type is a derived class, the function of the derived class is called, and if the object type is a base class, the function of the base class is called

1: A function declared with the virtual keyword is called a virtual function, and a virtual function is definitely a member function of a class.

2: A class with a virtual function has a one-dimensional virtual function table called a virtual table, and the object of the class has a virtual pointer to the beginning of the virtual table. A virtual table corresponds to a class, and a virtual table pointer corresponds to an object.

3: Polymorphism is a multi-interface implementation, is the core of object-oriented, divided into the polymorphism of the class and the polymorphism of the function.

4: Polymorphism is implemented by virtual function, which combines dynamic binding.

5: pure virtual function is virtual function plus = 0;

6: Abstract class refers to a class that includes at least one pure virtual function.

Pure virtual function: virtual void fun () = 0; abstract Class! This function must be implemented in a subclass, that is, there is a name first, no content, and the content is implemented in the derived class.

Implementation process:

At compile time, the compiler discovers that there are virtual functions in the Father class, at which point the compiler creates a virtual table (that is, vtable) for each class that contains the virtual function, which is a one-dimensional array in which the address of each virtual function is stored.

So how do you locate a virtual table? The compiler also provides a virtual table pointer (that is, vptr) for each object that points to the virtual table of the class to which the object belongs

When the program is running, initialize the vptr according to the type of the object, so that vptr correctly points to the virtual table of the owning class, so that when the virtual function is called, the correct function can be found.

Be aware that:

1. For virtual function calls, there is a virtual table pointer inside each object that is initialized to the virtual table of this class. So in a program, no matter how your object type is converted, but the virtual table pointer inside the object is fixed, it is possible to implement dynamic object function calls, which is the principle of C + + polymorphism implementation.

2. It is because the virtual function called by each object is indexed by a virtual table pointer, it is very important to determine the correct initialization of the virtual table pointer, in other words, we cannot call the virtual function until the virtual table pointer is properly initialized, so when or where is the virtual table pointer initialized?

The answer is to create the virtual table in the constructor and initialize the virtual table pointer, when constructing the subclass object, the constructor of the parent class is called, at which point the compiler only "sees" the parent class, does not know if there is any successor, it initializes the virtual table pointer of the parent class object, which points to the virtual table of the parent class. When the constructor of a subclass is executed, the virtual table pointer of the subclass object is initialized, pointing to its own virtual table.

Summary (the base class has virtual functions):

1: Each class has a virtual table

2: Virtual tables can inherit, if the subclass does not override the virtual function, then there will still be the address of the function in the virtual table of the subclass, except that the address points to the virtual function implementation of the base class, and if the base class has 3 virtual functions, then there are three items (virtual function addresses) in the virtual table of the base class, and the derived class will be empty, If the corresponding virtual function is overridden, the address in the virtual table will change, pointing to its own virtual function implementation, and if the derived class has its own virtual function, the item will be added in the virtual table.

3: The order of the virtual address in the virtual table of the derived class is the same as the virtual function address in the virtual table of the base class.

This is the C + + polymorphism, when the C + + compiler at compile time, found that the Father class say () function is a virtual function, this time C + + will use late binding technology, that is, the compiler does not determine the specific call function, but at run time, depending on the type of object to confirm which function is called, This ability is called C + + polymorphism, and when we do not add the virtual keyword before the Say () function, the C + + compiler determines which function is called, which is called early binding.

The polymorphism of C + + is realized by late binding technology.

C + + polymorphism is summed up in a nutshell: Before the function of the base class with the virtual keyword, override the function in a derived class, the runtime will invoke the corresponding function according to the actual type of the object, if the object type is a derived class, call the function of the derived class, if the object type is a base class, call the base class function.

A virtual function is defined in a base class to not determine the specific behavior of its derived class, for example:

Define a base class: Class Animal//animal, its function is breathe ()

Then define a class fish//fish. Its function is also breathe ()

Define a class Sheep//sheep, and its function is also breathe ()

Fish,sheep is defined as a derived class of animal, but fish is not the same as the breathe of sheep, so the base class cannot determine how breathe is defined, so only one virtual breathe is defined in the base class, which is an empty virtual function. The specific functions are defined separately in the subclass, program generally run, find the class, if it has a base class, and then find its base class, and finally run a function in the base class, then it found in the base class is the virtual identity of the function, it will go back to the subclass of the same name function, the derived class is called a subclass, the base class is also called the parent class, This is the creation of virtual functions, and the embodiment of the polymorphism of the class.

Polymorphism here refers to the polymorphism of a class.

The polymorphism of a function is a function that is defined as a function of several different parameters. When you call this function, different functions with the same name are called.

In general (no virtual functions are involved), when we invoke a function with a pointer/reference, the function being called depends on the type of the pointer/reference.

When designing to polymorphism, virtual functions and dynamic bindings are used, at which point the call is not determined at compile time but at runtime. Do not consider the pointer/reference type alone but look at the type of the pointer/reference object to determine the invocation of the function, based on the address of the function in the virtual table pointed to by the virtual pointer in the object, determining which function to call

C + + multi-state implementation principle

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.