2016-11-22 15 views
-4
template <typename T> 
class store // Very basic class, capable of accepting any data-type and does nothing too much 
{ 
    public: 
    store(T value) : value(value) {} 
    private: 
    T value; 
} 

template <> 
class store<int> // Inherits all the basic functionality that the above class has and it also has additional methods 
: public store<int> // PROBLEM OVER HERE. How do I refer to the above class? 
{ 
    public: 
    store(int value) : store<int>(value) /* PROBLEM OVER HERE. Should refer to the constructor of the above class */ {} 
    void my_additional_int_method(); 
} 

ここで私は継承に問題があります。ベースクラスはすべての派生クラスと同じ目的で使用されるため、ベースクラスの名前を変更したくない(唯一の違い - 派生クラスには余分なメソッドがほとんどない)テンプレートクラスの継承

+3

は***「thheコードセクションを説明するために、いくつかのコンテキストを追加(または...してくださいそれが見えます。いくつかの詳細を追加してください "***それで気付いたのですか? –

+3

あなた自身が継承するクラスを作ろうとしていますか?どういうことですか?これは特殊化の試みですか? –

+0

http://stackoverflow.com/q/ 27453449/560648 –

答えて

2

:あなたのポストは、ほとんどのコードであるllike

template <typename T> 
class store_impl 
{ 
    public: 
    store_impl(T value) : value(value) {} 
    private: 
    T value; 
} 

// default class accepting any type 
// provides the default methods 
template <typename T> 
class store: public store_impl<T> 
{ 
public: 
    store(T value) : store_impl(value) {} 
} 

// specialization for int with extra methods 
template <> 
class store<int>: public store_impl<int> 
{ 
    public: 
    store(int value) : store_impl<int>(value) 
    {} 
    void my_additional_int_method(); 
} 
+0

mixinを使用して専門分野のインターフェイスを拡張することは非常に良い考えです。 –

+0

ベストアンサーに選ばれました。より良い方法を見つけることができません – user3600124

0

クラスに名前を付けることはできません専門的なテンプレート:

class store_int : public store<int> 

またはtypedefまたはusing声明

012を使用します。

template <> 
class store<int> 

あなたが何ができるかは、それを具体的な型名を与えることですあなたは多分このような何か行うことができます

typdef store<int> store_int; 

using store_int = store<int>; 
関連する問題