Why a constructor cannot be a virtual function in C + +, a destructor is a virtual function

Source: Internet
Author: User

One, what is a virtual function?

Simply put, those member functions that are modified by the virtual keyword are virtual functions. The function of virtual function, the use of professional terminology to explain is to achieve polymorphism (polymorphism), polymorphism is the interface and implementation of the separation, with the image of the language is to achieve a common approach, but because of individual differences and adopt different strategies.

The so-called virtual function is a polymorphic case only executes one, and from the concept of inheritance, always first constructs the parent class object, then can be the child class object, if the constructor is set to the virtual function, then when you constructs the parent class's constructor to display the call constructs, Another reason is to prevent the wrong, imagine if you accidentally rewrite a subclass with the same function as the parent constructor, then the constructor of your parent class will be overwritten, that is, the construction of the parent class cannot be completed. It's going to go wrong.


The destructor must be a virtual function in some cases (for example, if you want your class to be inherited, then the destructor of this class is best virtual, inheriting a destructor is not a virtual class is risky), it is worth adding that when the function of the base class is a virtual function, the overloaded function of the subclass is also virtual, Even without the virtual keyword is virtual.

If the child class overrides the virtual function of the parent class
A pointer to a parent class or reference to an object that actually points to a subclass or to its own object
When a virtual function is called through the pointer or reference, the (virtual) function of the subclass is called

If it is not a virtual function, the function of the parent class will be called

 class   a{ public  :  virtual  void  print () {cout<< "This is  A" <<endl;} //  Now it's a virtual function   class  B:public   a{ public  :  void  print () {cout<< "This is  B" <<endl;} //  };  

Undoubtedly, the member function print () of Class A has become a virtual function, so is the print () of class B a virtual function? The answer is yes, we simply set the member function of the base class to virtual, and the corresponding function of its derived class automatically becomes a virtual function. Therefore, the print () of Class B also becomes a virtual function. If you need to modify the virtual keyword before the corresponding function of the derived class, it is your own problem.

Now rerun the MAIN2 code so that the result of the output will be this is a and this is B.

int // main2 A; b b; A* p1=&A; A* p2=&b;p1,print ();p 2,print ();}

Destructors should be defined as virtual functions, or memory leaks are prone to occur!

1. Why can't a constructor be a virtual function?

The call to a virtual function requires a virtual function table pointer, which is stored in the object's content space, and if the constructor is declared as a virtual function, there is no memory space because the object has not yet been created, and there is no virtual function table address to invoke the virtual function-constructor.    2. Why can destructors be virtual, and what might be the problem if they are not set as virtual functions?    First the destructor can be a virtual function, and when you want to use a base class pointer or reference to call a subclass, it is best to declare the destructor of the base class as a virtual function, otherwise there can be a memory leak problem. Example: Subclass B inherits from base class A; A *p = new B; Delete p;1) At this point, if the destructor of Class A is not a virtual function, then delete p; will simply call A's destructor, releasing only part A of the B object, and the new part of the derivation is not freed. 2) If the destructor of Class A is a virtual function, delete p;    The destructor of B is called first, and then A's destructor is called to release all the space for the B object. Supplement: b *p = new B; Delete p; The destructor of B is called first, and then A's destructor is called. 

[From]

Http://zhidao.baidu.com/question/32424351.html

Http://www.cnblogs.com/zhice163/archive/2012/07/10/2584835.html

Why a constructor cannot be a virtual function in C + +, a destructor is a 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.