Why not define member functions as template virtual functions in C++
This article introduces the knowledge of why member functions should not be defined as template virtual functions in C++. Many people will encounter such a dilemma in the operation of actual cases. next, let the editor lead you to learn how to deal with these situations! I hope you can read it carefully and be able to achieve something!
T.83: do not define member functions as template type virtual functions
Reason (reason)
C++ does not support that. If it did, vtbls could not be generated until link time. And in general, implementations must deal with dynamic linking.
C++ does not support this. If supported, a virtual function table can only be generated when you go to a link. In general, the implementation of the C++ language must deal with dynamic links.
Example, don't (negative example)
Class Shape {
/ /...
Template
Virtual bool intersect (T* p); / / error: template cannot be virtual
}; Note (note)
We need a rule because people keep asking about this
Because people keep asking this question, we need such a rule.
Alternative (other options)
Double dispatch, visitors, calculate which function to call
Double distribution, visitors, calculate by calling functions.
Enforcement (implementation recommendations)
The compiler handles that.
Deal with this problem through the compiler.
This is the end of the content of "Why not define a member function as a template virtual function in C++". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!