2016-10-12 8 views
-1

私は、ブーストマルチインデックスコンテナの機能を継承する基本コンテナクラスを作成する方法を探しています。私は、この基本クラスに他の関数を追加し、この基本クラスの関数を使用し、boostのマルチインデックスコンテナを利用できる他のクラスを作成できるようにします。C++でBoostマルチインデックスコンテナを継承する方法はありますか?

私のような何か試してみました:

template < class D, E > 
class BoostModelContainer : public boost::multi_index_container<D, E> 
{ 
public: 
    D* AddItem(const D& item) 
    { 
     //code here 
    } 
}; 

をしてからのような基本クラスを継承し、他のクラスを作成しました:

class ExampleContainer : public BoostModelContainer< CItem, 
boost::multi_index::indexed_by< 
    boost::multi_index::ordered_unique< 
    boost::multi_index::tag<id_tag>, boost::multi_index::member< CItem, ItemId, &CItem::m_id > >, 
    boost::multi_index::ordered_unique< 
    boost::multi_index::tag<name_tag>, boost::multi_index::member< CItem, String, &CItem::m_name > > 
    > 
> 

しかし、これはコンパイルされないでしょう。誰か他のアイデアを持っているか、これを動作させる方法を知っていますか?

ありがとうございました!

+1

ドン」あなたの特定のケースを十分に知っていますが、とにかくこれをここにドロップします:[相性よりも組成を優先しますか?](http://stackoverflow.com/questions/49002/prefer-composition-over-inheritance) – user4581301

+1

私はしません構図はうまくいくと思いますか?私は、異なるマルチインデックスキー構造を持つ多くのさまざまなコンテナを作成しようとしています。そして、私はすべてのコンテナが各コンテナに関数を置くのではなく、基本関数にアクセスできるようにしたい。そして私はそれぞれのコンテナをマルチインデックスコンテナにすることを望んでいます。 –

+1

これはちょうど "あなたの選択肢を検討する"コメントです。一方通行か他方通話かを問うには十分ではありません。コンテナからの継承が適切な場合もありますが、通常は継承が適切ではありません。 – user4581301

答えて

2

テンプレート引数を2つ渡していますが、テンプレートには1つのみが使用されます。

template < class D, class E > 
class BoostModelContainer : public boost::multi_index_container<D, E> 
{ 
public: 
    D* AddItem(const D& item) 
    { 
     //code here 
    } 
}; 
+0

追加のテンプレート引数ではまだコンパイルされません。 –

+0

D:¥Code¥boost¥boost_1_62_0¥boost/mpl/aux_/has_begin.hpp(20):エラーC2988:認識できないテンプレート宣言/定義 –

+0

問題がコードの他の場所にあることになりました! –

2

それはあなたの問題がminimal, complete and verifiable exampleなしで正確に何把握するために少し難しいです。しかし、右折の答えは、メインのコードに問題があることを間違いなく指摘しています。基本的に、あなたのtemplate宣言でclassキーワードが含まれていない。それとは別に

template < class D, class E > // missing ---------^^^^^ class BoostModelContainer : public boost::multi_index_container<D, E> { /* ... */ }; 

が、それは次のよう完全コード例が示すように、(compiles fine, see live demoを)うまく動作するはずです:

#include <boost/multi_index_container.hpp> 
#include <boost/multi_index/member.hpp> 
#include <boost/multi_index/ordered_index.hpp> 
#include <string> 

template <class D, class E> 
class BoostModelContainer : public boost::multi_index_container<D, E> 
{ 
public: 
    D* AddItem(const D& item) 
    { 
    //code here 
    } 
}; 

struct Foo 
{ 
    int id; 
    std::string name; 
}; 

struct id_tag { }; 
struct name_tag { }; 

// Requires -std=c++11  
using ExampleContainer = BoostModelContainer< 
    Foo, 
    boost::multi_index::indexed_by< 
    boost::multi_index::ordered_unique< 
     boost::multi_index::tag<id_tag>, boost::multi_index::member<Foo, int, &Foo::id> >, 
    boost::multi_index::ordered_unique< 
     boost::multi_index::tag<name_tag>, boost::multi_index::member<Foo, std::string, &Foo::name> > 
    > 
    >; 

// works with c++03 as well 
struct ExampleContainer2 : public BoostModelContainer< 
    Foo, 
    boost::multi_index::indexed_by< 
    boost::multi_index::ordered_unique< 
     boost::multi_index::tag<id_tag>, boost::multi_index::member<Foo, int, &Foo::id> >, 
    boost::multi_index::ordered_unique< 
     boost::multi_index::tag<name_tag>, boost::multi_index::member<Foo, std::string, &Foo::name> > 
    > 
    > 
{ }; 

int main() 
{ 
    ExampleContainer ec1; 
    ExampleContainer2 ec2; 
} 
+0

問題はコード内の別の場所にあることになりました! –

関連する問題