Which functions cannot be declared as virtual functions in C ++?

Source: Internet
Author: User

Common functions that cannot be declared as virtual functions include common functions (non-member functions), static member functions, inline member functions, constructor functions, and youyuan functions.

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

A normal function (non-member function) can only be overload and cannot be override. it is meaningless to declare it as a virtual function. Therefore, the compiler will specify a function during compilation.

During the multi-state runtime, the behavior is reflected in the virtual function. The virtual function is inherited to reflect the multi-state function. The top layer

A function does not belong to a member function and cannot be inherited.2. Why does C ++ not support constructor as a virtual function?

The reason is very simple, mainly in terms of semantics, so it is not supported. This is because the constructor was originally created to explicitly initialize object members. However, virtual functions are mainly used to process objects correctly without fully understanding the details. In addition, virtual functions generate different actions for different types of objects. Currently, objects are not generated. How to use virtual functions to complete the actions you want to complete. (This is not a typical paradox)

1) constructor cannot be inherited, so it cannot be declared as a virtual function.

(2) constructor is generally used to initialize objects. polymorphism can be realized only after an object is generated.

Function. If the constructor is declared as a virtual function, the result is that the object has not been generated.

In this case, the wine adopts a polymorphism mechanism, and thus does not work.

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

In fact, it is very simple. The inline function is designed to expand directly in the Code and reduce the cost of function calling. The virtual function is designed to accurately execute its own actions after inheritance, this cannot be unified. (Besides, the inline function is expanded during compilation, and the virtual function can be dynamically bound during runtime)

Inline functions are essentially different from virtual functions. inline functions are expanded when a program is compiled and replaced by the entire function body during function calls, virtual functions can determine how to call them at runtime. Therefore, inline functions reflect a compilation mechanism, while virtual functions reflect a runtime mechanism. In addition, all virtual functions cannot be inline functions.

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

This is also very simple. static member functions only have one copy of code for each class. All objects share the code, and they do not need to be dynamic. It cannot be inherited. It only belongs to this class.

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

Because c ++ does not support the inheritance of functions, there is no virtual function for functions without inheritance features. A friend function does not belong to a class member function and cannot be inherited.

Eg:


 

/** Main. CPP ** created on: 2012-11-17 * Author: China */# include <iostream> using namespace STD; Class B {public: B () {cout <"base class constructor" <Endl;}/* in class inheritance, if a base class Pointer Points to a derived class, when the base class pointer is deleted, if it cannot be defined as a virtual function, the part derived from the derived class cannot be analyzed. * You can remove it from virtual. * therefore, in the class inheritance system, the destructor of the base class are not declared as virtual functions, which may cause memory leakage. Therefore, if you design a base class, you must declare it as a virtual function. **/Virtual ~ B () {cout <"base class destructor" <Endl;} virtual void func () {cout <"base class func ()" <Endl ;} private :}; Class D: Public B {public: D () {cout <"derived class structure" <Endl ;}~ D () {cout <"" <Endl;} void func () {cout <"func ()" <Endl ;} PRIVATE:}; int main (INT argc, char ** argv) {d; // The call construction first has the object B * P = & D; P-> func (); // re-embody the polymorphism mechanism P = new D (); // re-call the construction p-> func (); // re-embody the polymorphism mechanism Delete P; return 0 ;}

 

 

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.