问题描述
我想让这个专门的不改变主.是否可以根据其基类专门化某些东西?我希望如此.
I want to make this specialized w/o changing main. Is it possible to specialize something based on its base class? I hope so.
-编辑-
我将有几个继承自 SomeTag 的类.我不想为他们每个人编写相同的专业.
I'll have several classes that inherit from SomeTag. I don't want to write the same specialization for each of them.
推荐答案
这篇文章描述了一个巧妙的技巧:http://www.gotw.ca/publications/mxc++-item-4.htm
This article describes a neat trick: http://www.gotw.ca/publications/mxc++-item-4.htm
这是基本思想.您首先需要一个 IsDerivedFrom 类(它提供运行时和编译时检查):
Here's the basic idea. You first need an IsDerivedFrom class (this provides runtime and compile-time checking):
那么你的 MyClass 需要一个潜在的特殊实现:
Then your MyClass needs an implementation that's potentially specialized:
和 MyClass 实际上看起来像:
and MyClass actually looks like:
那么你的主菜就可以了:
Then your main will be fine the way it is:
这篇关于基于继承类的模板特化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!