C ++ pure virtual function call

Source: Internet
Author: User

Before reading this article, you need to understand the basic usage of the C ++ virtual function and how the C ++ virtual function is implemented. This is the basic content and is not covered in this article. During the last intern interview, the interviewer asked me how to implement the C ++ virtual function. People who want to read the book Inside the C ++ Object Model are familiar with this. During the explanation, he asked me what pure virtual functions are, what to do. In the answer process, I briefly mentioned that "the pure virtual function of C ++ may be called in special circumstances, the specific behavior is determined by the implementation of the C ++ standard library. "I thought back and forth about this sentence and thought about the specific call for a long time. Fortunately, the interviewer did not ask this question, otherwise, I had to talk about it. (at that time, almost the whole process was filled with endless answers, and the interviewer was always in the "hmm" status ). When I am idle and do not want to review the exam, I will write code and summarize a blog article to discuss this issue with you. First, it must be clear that pure virtual functions should not be called! Because pure virtual functions are used to define interfaces, sometimes the base class cannot find a reasonable implementation, so it is declared in the form of virtual functions, so that its subclass can be implemented in a specific way. Therefore, if a pure virtual function is called, it must be a logical error in your program. This is something we need to understand and avoid in our work, this is one of the purposes of this article. As you know, virtual functions are called through pointers or references. The actual number of called functions is determined by the actual number of pointers/references. The actual object can be determined by the first value in the memory block of the object, vptr, pointing to the pointer of the virtual function table. Pure virtual functions belong to the base class, so to call pure virtual functions, the object referred to by this pointer must be a base class. However, the objects of abstract classes (classes containing pure virtual functions) cannot be defined by users. Well, this provision seems rigorous, how can C ++ allow you to call pure virtual functions? You are also curious about this issue. User-Defined abstract class objects are not allowed. Yes, this object cannot be constructed! Remember that the object construction process is to call the base class constructor first, then call the constructor of the subclass, that is, to construct the base class Object first, and then construct the subclass object, I will not mention the object structure. That is to say, any virtual function called in the base class constructor calls the base class's own virtual function instead of the subclass's virtual function. Oh! The vulnerability is here!

Base {
:
Foo () =;
Base () {foo ();}};
Derived: Base {
Foo (){}
};
Main (){
Derived d;
} Fortunately, the compiler can find errors and warn you about them. The following is my use of CodeBlocks (which comes with MinGw, including the gcc 4.7.1-based compiler) A compilation warning and a link error: Compilation warning: pure virtual 'virtual void Base: foo () 'called from constructor Link error: undefined reference to 'base :: foo () 'but unfortunately, the code in actual applications is often much more complex, making it impossible for the compiler to find problems during compilation. Modify the above Code as follows to successfully call the virtual function: Base {
:
Foo () =;


};
 
Derived: Base {
Foo (){}
};
 
Main (){
Derived d;
} The result after running is:

 


Pony279 original blog, reprinted please indicate the source http://www.cnblogs.com/Pony279/archive/2013/06/04/3117955.html

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.