Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why not define member functions as template virtual functions in C++

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report