子类作为专业化 - 即:在专业化中添加一个方法 [英] subclass as specialization - ie: adding a method in the specialization

查看:106
本文介绍了子类作为专业化 - 即:在专业化中添加一个方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为模板的专业化添加一个额外的转换运算符

I want to add an additional conversion operator to a specialization of a template

- spelcialization是否从其主模板继承所有方法?

-can a spelcialization inherit all methods from its main template ?

template<class T>
MyClass
{
public:
    operator Foo() { return(getAFoo()); }
};


template<>
MyClass<Bar>
{
public:
    // desire to ADD a method to a specialization yet inherit
    // all methods from the main template it specializes ???
    operator Bar() { return(getABar()); } 
};


推荐答案

模板特殊化是不同类型,因此不共享功能。

Template specializations are distinct types and thus don't share functions.

您可以通过继承一个公共基类来获取共享功能:

You can get shared functionality by inheriting from a common base class:

template<class T>
struct Base {
    operator Foo() { return Foo(); }
};

template<class T>
struct C : Base<T> {
    // ...
};

template<>
struct C<Bar> : Base<Bar> {
    // ...
    operator Bar() { return Bar(); }
};

这篇关于子类作为专业化 - 即:在专业化中添加一个方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆