C ++ virtual functions

Source: Internet
Author: User

C ++ virtual functions

1 #include<iostream> 2 using namespace std; 3 class Base{ 4 public: 5     void m() 6     { 7         cout << "it's Base's m() " << endl; 8     } 9     virtual void f()10     {11         cout << "it's Base's f()" << endl;12     }13 };14 class Sub: public Base 15 {16     void m()17     {18         cout << "it's Sub's m()" << endl;19     }20     void f()21     {22         cout << "it's Sub's f()" << endl;23     }24 };25 void main()26 {27     Base* base = new Sub;28     base->f();29     base->m();30 }

The above is based on the Base class, And Sub derives the Base. At the same time, two functions f () and m () are rewritten. When the main function uses a Base type pointer to point to a Sub type object, at this time, the Base pointer is used to call f () and m (), so the problem arises. By default, the Base class pointer calls the method of the current type, that is, Base: f (), Base: m (), so all the statements in the Base method should be output, but why is the result output? Because f () of the parent class is a virtual function, when the base class pointer calls its derived class object, the corresponding method of the polymorphism object is called by default, so I explained why Base * base-> f () calls the f () method of the derived class Sub. From this, we can know that the Virtual function is mainly used to solve the problem of polymorphism, because after a base class method is defined, its derived class object will implement different overwrites for this method, that is, polymorphism, if you use the base class pointer to point to the polymorphism subclass of the base class at this time, you need to use virtual to modify the base class method, in this way, you can call the method of the required derived class to avoid calling this method of the base class. Virtual is an important keyword in the C ++ OO mechanism. The function that adds the Virtual keyword to the class Base is a Virtual function (such as the function print ), therefore, in the Base-Derived class Derived, You can override the virtual function to overwrite the Base-class virtual function. When the Base class Base pointer point points to the object of the Derived class Derived, the call to the print function of the point actually calls the print function of the Derived instead of the print function of the Base. This is the embodiment of polymorphism in the object-oriented model. There is also a virtual function destructor. The application has the following problem: when the base class is an abstract class and the base class Pointer Points to the derived class, the first case is to delete the memory that the base class Pointer Points.
 1 de<iostream> 2 using namespace std; 3 class Base{ 4 public: 5     Base() 6     { 7     } 8     ~Base() 9     {10         cout << "Base has deleted" << endl;11     }12 };13 class Sub: public Base 14 {15 public:16     Sub()17     {18     }19     ~Sub()20     {21         cout << "Sub has delete" << endl;22     }23 };24 void main()25 {26     Base* base = new Sub;27     delete base;28     base = NULL;29 } 

 

In this case, the Base class Base pointer is used to operate the inherited class Sub. After the delete Base * pointer, we can get the result that only the destructor of the Base class are called, however, the destructor of the derived class is not called. This is equivalent to deleting half of the memory and deleting half of the memory, resulting in Memory leakage. In this case, you need to use the virtual destructor, to call the destructor of the derived class to delete the memory space, that is, the following situation:
 1 #include<iostream> 2 using namespace std; 3 class Base{ 4 public: 5     Base() 6     { 7     } 8     virtual ~Base() 9     {10         cout << "Base has deleted" << endl;11     }12 };13 class Sub: public Base 14 {15 public:16     Sub()17     {18     }19     ~Sub()20     {21         cout << "Sub has delete" << endl;22     }23 };24 void main()25 {26     Base* base = new Sub;27     delete base;28     base = NULL;29 }

 

At this point, we can see that after the basic class destructor is added with the Virtual function, the destructor of the base class and the derived class are successfully called, this completely releases the base class pointer to the memory. The method by which a virtual destructor works is: The destructor of the bottom-level derived class is called first, and the destructor of each base class is called. However, when a class does not have a virtual function, this class will not be treated as a base class. Defining a virtual destructor will only increase the memory overhead, because you need to enable a virtual function list for storage.

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.