문제

In my application I have the following classes:

class transaction
{
public:
   .....
}

class src_transaction: public transaction
{
public:
   .....
}

class dst_transaction: public transaction
{
public:
   .....
}

I want to construct a bimap, which will take an integer number and a pointer to either src_transaction or dst_transaction.

How can I do that using Boost libraries? Should I declare the "class transaction" as enable_shared_from_this? If so, should it be unqiue_ptr or shared_ptr? Because I want to do some operation on the biamp inside either src_transaction or dst_transaction as following:

 bimap.inset(12, "Pointer to the current transaction i.e. from the same class");
도움이 되었습니까?

해결책

You should try something like this:

   #include <iostream>
   #include <boost/bimap.hpp>
   #include <boost/shared_ptr.hpp>
   #include <boost/make_shared.hpp>


    using namespace std;

    class Base{

     public:
       Base(int val):_value(val){}
       int _value;

    };


    class Derv1:public Base{

      public:
         Derv1(int val):Base(val){}

    };


    class Derv2:public Base{

      public:
         Derv2(int val):Base(val){}

    };

  //typedef boost::bimap< int,Base* > bm_type;
    typedef boost::bimap< int,boost::shared_ptr<Base> > bm_type;
    bm_type bm;

    int main()
    {

    //  bm.insert(bm_type::value_type(1,new Derv1(1)));
    //  bm.insert(bm_type::value_type(2,new Derv2(2)));

     bm.insert(bm_type::value_type(1,boost::make_shared<Derv1>(1)));
     bm.insert(bm_type::value_type(2,boost::make_shared<Derv2>(2)));

      cout << "There are " << bm.size() << "relations" << endl;

      for( bm_type::const_iterator iter = bm.begin(), iend = bm.end();
          iter != iend; ++iter )
      {
        // iter->left  : data : int
        // iter->right : data : Base*

        cout << iter->left << " <--> " << iter->right->_value << endl;
      }

    }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top