Under C + + topics

Source: Internet
Author: User
Tags function prototype

1) What is the output of the following two segments of code?
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M00/84/89/wKiom1eTZoWBbo8PAAEmvKHJ11E825.png "title=" QQ picture 20160723204307.png "alt=" Wkiom1etzowbbo8paaemvkhj11e825.png "/>

(1) First, Print in Base

Print in Derive

(2) Second, Print in Base

Print in Base

2) Brief introduction to the function of C + + virtual function and the underlying implementation principle

function: Simply put, those member functions that are modified by the virtual keyword are virtual functions. virtual function to achieve polymorphism (polymorphism), polymorphism is to separate the interface and implementation, is to achieve a common approach, but because of individual differences and adopt different strategies.

When a member function operation is generalized, when a pointer to a base class points to an object of a different derived class, the base class pointer calls its virtual member function, and the member function that actually points to the object is called, not the member function defined in the base class (as long as the derived class overrides the member function). If it is not a virtual function, it will call the function defined in the base class, regardless of which derived class object the base class pointer points to.

Virtual functions are the basis of dynamic binding. A non-static member function. In the declaration of the class, write virtual before the function prototype. Virtual is used only to describe prototypes in a class declaration and cannot be used when a function is implemented. is inherited, a virtual function is declared in the base class, and the same prototype function is automatically a virtual function, whether or not it is described in a derived class. Essence: Overrides are not overloaded declarations. Invocation: By a base-class pointer or reference, execution determines which function is called based on the class of the object that the pointer is pointing to.

The underlying implementation: when a virtual function exists in a class, the compiler inserts a piece of data that you don't know and creates a table for them. That data is called the vptr pointer, pointing to that table. The table is called VTBL, each class has its own VTBL,VTBL function is to save the address of the virtual function in its own class, we can consider the VTBL image as an array, each element of this array is the address of the virtual function. When other classes inherit this class,

3) which is faster for an object to access ordinary member functions and virtual functions?

Accessing member functions is faster, because when a virtual function is called, the virtual table must be accessed by a virtual pointer to obtain the address of the function, called the dynamic polymorphism. The normal member function, at compile time, has determined the address of the function.

4) Under what circumstances, destructors need to be virtual functions?

When it is inherited and there may be pointers in subclasses. (this pointer needs to free up space at the time of the destructor)

When a destructor is declared as a virtual function, the base-class pointer can point to the object of the derived class (polymorphism), and if the pointer is deleted, the derived class destructor that the pointer points to is called, and the destructor of the derived class automatically calls the destructor of the base class, so that the object of the entire derived class is completely freed. If a destructor is not declared as a virtual function, the compiler enforces a static binding, and when the base class pointer is deleted, only the destructor of the base class is called instead of the derived class destructor, which results in incomplete destruction of the derived class object. Therefore, it is necessary to declare destructors as virtual functions.

5) Can an inline function, constructor, or static member function be a virtual function?

It's not possible.

Note: inline, static, constructor three functions cannot have the virtual keyword.

Inline is a compile-time expansion and must have an entity;

Static belongs to class itself and must have an entity;

The virtual function is based on the vtable (memory space), constructor function if it is virtual, the call also needs to be based on vtable search, but constructor is not found in the case of virtual, Because constructor itself does not exist, create instances of class, no instances, class members (except public static/protected static for friend Class/functions, The rest, whether virtual or not, cannot be accessed.

 those in C + + functions that cannot be declared as virtual functions

Common functions that cannot be declared as virtual functions are: ordinary function (non-member function), static member function, inline member function, constructor, friend function.

1. Why C + + does not support normal functions as virtual functions?

The normal function (non-member function) can only be overload, cannot be override, declared as virtual function also has no meaning, so the compiler will compile the Shibangding function.

2. Why C + + does not support constructor functions as virtual functions?

The reason is very simple, mainly from the semantic considerations, so it is not supported. Because constructors are originally created to explicitly initialize object members, virtual function is primarily designed to handle objects correctly without fully knowing the details. In addition, the virtual function produces different actions on different types of objects, and now the object has not been generated, how to use the virtual function to complete the action you want to complete. (This is not a typical paradox)

3. Why C + + does not support inline member functions as virtual functions?

In fact, it is very simple, the inline function is to expand directly in the code, reduce the cost of function calls, virtual function is to inherit after the object can accurately perform their own actions, it is impossible to unify. (again, the inline function is expanded at compile time , and virtual functions can be dynamically determined at runtime )

4. Why C + + does not support static member functions as virtual functions?

This is also very simple, static member functions for each class has only one copy of the code, all the objects share this code, he also did not want to dynamically state the necessity.

5. Why C + + does not support friend functions as virtual functions?

Because C + + does not support the inheritance of friend functions, there is no virtual function for functions that do not inherit attributes .

6) Can a virtual function be called in a constructor?  

The reason is very simple, mainly from the semantic considerations, so it is not supported. because constructors are originally created to explicitly initialize object members, virtual function is primarily designed to handle objects correctly (incomplete objects) without fully knowing the details. In addition, the virtual function produces different actions on different types of objects, and now the object has not been generated, how to use the virtual function to complete the action you want to complete. (This is not a typical paradox).

This article is from the "Small Stop" blog, please be sure to keep this source http://10541556.blog.51cto.com/10531556/1829195

Under C + + topics

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.