The essence of C + +-virtual function

Source: Internet
Author: User

Virtual functions are defined in the base class for overloading and polymorphic needs, even if the definition is empty, so you can override or write functions in the base class in subclasses!

Pure virtual functions are not defined in the base class and must be implemented in subclasses, much like the interface functions in Java!

Virtual functions

Introduction Reason: In order to facilitate the use of polymorphic properties, we often need to define virtual functions in the base class.

Class Cman

{

Public

virtual void Eat () {...};

void Move ();

Private

};

Class Cchild:public CMan

{

Public

virtual void Eat () {...};

Private

};

CMan M_man;

Cchild M_child;

This is the essence of the use, if you do not define the base class pointer to use, not much significance

CMan *p;

p = &m_man;

P->eat (); The Eat member function of Cman is always called and does not call Cchild's

p = &m_child;

P->eat (); If the subclass implements (Overrides) the method, the Eat function of Cchild is always called

The Eat method of the Cman is not called, and if the subclass does not implement the function, the Eat function of the Cman is called

P->move (); There is no such member function in the subclass, so it is called in the base class.

Pure virtual function

Reason for introduction:

1, with "virtual function";

2, in many cases, the base class itself is unreasonable to generate objects. For example, animals as a base class can be derived from tigers, peacocks and other sub-categories, but the animals themselves generated objects are obviously unreasonable.

Pure virtual function is the base class only defines the function body, there is no implementation process definition method as follows

virtual void Eat () = 0; Direct =0 don't define it in CPP.

Pure virtual functions are equivalent to interfaces, cannot be directly instantiated, and require derived classes to implement function definitions

Some people might be wondering, what's the use of defining these, I think it's useful.

For example, if you want to describe the attributes of something to someone else, and you don't want to achieve it, you can set

Righteousness is pure virtual function. Speak a little more thoroughly. Like building a building, you're the boss, you give the construction company

Describe the characteristics of your building, how many floors, the roof of a garden or something.

The construction company can do it your way, and if you don't make that clear, you might be building

The company doesn't know much about the nature of the building you need. With the pure need function, we can work very well.

Difference between virtual function and pure virtual function

Opinion one:

Class is declared as a virtual function, this function is implemented, even if it is an empty implementation, its role is to allow this function in its subclasses can be overloaded, so that the compiler can use late binding to achieve polymorphism

Pure virtual function is only an interface, is a function of the declaration, it is to be left in the subclass to achieve.

Class a{

Protected

void foo ();//General class function

virtual void foo1 ();//virtual function

virtual void Foo2 () = 0;//pure virtual function

}

Opinion two:

Virtual functions can also not be overloaded within subclasses, but pure virtual must be implemented in subclasses, just like Java interfaces. Usually we add a lot of functions to virtual, is a good habit, although sacrificing some performance, but increased the object-oriented polymorphism, because you can hardly anticipate the parent class inside the function is not to modify its implementation of the child class

Point three:

The classes of virtual functions are used for "implement inheritance", inheriting the interface and inheriting the implementation of the parent class. Of course, we can also complete their own implementation. The classes of pure virtual functions are used for "interface inheritance" and are mainly used in communication protocols. The focus is on the unity of the interface, which is accomplished by subclasses. In general, there are only pure virtual functions in the interface class.

Opinion four:

Error: A class with a pure virtual function is called a virtual base class, which cannot be used without directly generating an object, but only if it is inherited and overrides its virtual function. Such a class is also called an abstract class.

Virtual functions are for inheriting interfaces and default behavior

A pure virtual function simply inherits an interface, and the behavior must be redefined

////////////////////////////////////////////////////////////////////////////////////

The initialization of virtual base classes is syntactically identical to the initialization of virtual base classes, but constructors are called in different order.

The calling order of a derived class constructor has three principles:

(1) The constructor of the virtual base class is called before the non-virtual base class;

(2) If more than one virtual base class is included in the same level, the constructor of these virtual base classes

Differences between virtual and non-virtual base classes

Numbers are called in the order they are described;

(3) If the virtual base class is derived from a non-virtual base class, the base class constructor is still called first, and then the constructor of the derived class is called.

Edit the virtual base class for C + + in this paragraph when the derived class inherits the base class, and a virtual keyword inherits from the dummy base class, such as:

Class Derive:virtual Public Base

{

};

Virtual base class mainly solves in multiple inheritance, the base class may be inherited multiple times, the virtual base class mainly provides a base class to the derived class, such as:

Class B

{

};

Class D1:public B

{

};

Class D2:public B

{

};

Class C:public D1,public D2

{

};

Here c is inherited on D1,D2, but there are two base classes that cause confusion. Thus, the virtual base class is used, namely:

ClassB

{

};

Class D1:virtual Public B

{

};

Class D2:virtual PUBLICB

{

};

Class C:public D1,public D2

Edit this paragraph in the use of virtual base classes to note: (1) A class can be used in a class family both as a virtual base class, also used as a non-virtual base class.

(2) In the object of a derived class, a virtual base class with the same name produces only one virtual base class sub-object, and a non-virtual base class produces its own child object.

(3) The virtual base class sub-object is initialized by the constructor of the furthest derived class by calling the virtual base class's constructor.

(4) The farthest derived class refers to the class that is specified when an object is created in an inheritance structure.

(5) A call to the virtual base class constructor must be listed in the member initialization list of the constructor for the derived class, or the default constructor for the virtual base class if it is not listed.

(6) A call to a virtual base class constructor is listed in the member initialization list of a constructor in a derived class that derives directly or indirectly from a virtual base class. However, the constructor of the virtual base class is called only by the constructor of the furthest derived class that establishes the object, and the call to the virtual base class's constructor listed in all base classes of the derived class is ignored in execution, guaranteeing that the virtual base class sub-object is initialized only once.

(7) When a call to a virtual base class and a non-virtual base class constructor occurs at the same time in a member initialization list, the constructor of the virtual base class is executed before the constructor of the non-virtual base class.

Static linking: A call that can be determined during the program link phase.

Dynamic linking: A call that can be determined when the program executes.

The essence of C + +-virtual function

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.