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 templated class inheritance in C++

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

Share

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

This article mainly explains why there is no template inheritance in C++. Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn why there is no template inheritance in C++.

T.80: don't naively template class inheritance

Reason (reason)

Templating a class hierarchy that has many functions, especially many virtual functions, can lead to code bloat.

Templating contains many member functions, especially the class inheritance hierarchy of virtual functions can lead to code ballooning.

Example, bad (negative example)

Template

Struct Container {/ / an interface

Virtual T* get (int I)

Virtual T* first ()

Virtual T* next ()

Virtual void sort ()

}

Template

Class Vector: public Container {

Public:

/ /...

}

Vector vi

Vector vs

It is probably a bad idea to define a sort as a member function of a container, but it is not unheard of and it makes a good example of what not to do.

Defining a sort member function for a container is almost certainly a bad idea, but it's not without precedent and can be a good example of what we shouldn't do.

Given this, the compiler cannot know if vector::sort () is called, so it must generate code for it. Similar for vector::sort (). Unless those two functions are called that's code bloat. Imagine what this would do to a class hierarchy with dozens of member functions and dozens of derived classes with many instantiations.

When the editor accepts this code, there is no way to know if vector::sort () has been called, so code must be generated for it. The same is true of vector::sort (). As long as these two functions are not called, this is a kind of code bloat. Imagine what would happen if this happened to an inheritance structure that contained dozens of member functions and dozens of derived classes that were instantiated multiple times.

Note (Note)

In many cases you can provide a stable interface by not parameterizing a base; see "stable base" and OO and GP

In many cases, you can provide a stable interface without parameterizing the base class; see "stable Base Class and OO and GP."

Enforcement (implementation recommendations)

Mark virtual functions that depend on template parameters.

At this point, I believe you have a deeper understanding of "Why not template inheritance in C++". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

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