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

Source: Internet
Author: User

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 does C + + not support ordinary 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 does C + + not support a constructor function as a virtual function?

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 does C + + 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 does C + + 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 does C + + not support friend function as virtual function?

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

1, the top-level function : The behavior of the polymorphic operation is embodied in the virtual function, virtual function through inheritance to reflect the multi-state function, the top-level function does not belong to the member function, it can not be inherited.

2. Constructors : (1) constructors cannot be inherited and cannot be declared as virtual functions.

(2) Constructors are generally used to initialize objects, only after an object is generated, to play a polymorphic role, if the constructor is declared as virtual function, it is shown that the object has not been generated in the case of the use of polymorphic mechanisms, it is not feasible, the following example:

[CPP]View Plaincopy
  1. #include <iostream>
  2. Using namespace std;
  3. Class B
  4. {
  5. Public
  6. B () {}
  7. virtual void Show ()
  8. {
  9. cout<<"* * *" <<endl;
  10. }
  11. };
  12. Class D: PublicB
  13. {
  14. Public
  15. D () {}
  16. void Show ()
  17. {
  18. cout<<"= = =" <<endl;
  19. }
  20. };
  21. int main (void)
  22. {
  23. B *PB;
  24. D D; //Mr. Cheng Object
  25. pb=&d;
  26. Pb->show (); //re-expression polymorphism
  27. pb=new D (); //Call constructor first
  28. Pb->show (); //re-polymorphic
  29. Delete PB;
  30. return 0;
  31. }

3. Static function: cannot be inherited, only belongs to this class.

4, friend function: The friend function does not belong to the class member function, cannot be inherited.

5, the inline function: the inline function and the virtual function have an essential difference, the inline function is expanded when the program is compiled, at the function call with the entire function body to replace, and the virtual function is in the runtime to determine how to call, Thus, the inline function embodies a compile-time mechanism, and the virtual function is a kind of running-time mechanism. In addition, all virtual functions cannot be inline functions.

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

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.