Boost logo

Boost Users :

Subject: [Boost-users] [multi_index] How to use a class instead of a struct with the boost:multi_index_container? (addendum)
From: jamaj (jamajbr_at_[hidden])
Date: 2010-11-14 18:31:01


Hi All,

Since I posted the previous message, I made some experiments. First, I
changed the employee from struct to class:

From

struct employee
{
  int id;
  std::string name;
  int age;

  employee(int id_,std::string name_,int
age_):id(id_),name(name_),age(age_){}

  friend std::ostream& operator<<(std::ostream& os,const employee& e)
  {
    os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
    return os;
  }
};

to

class employee
{
  int id;
  std::string name;
  int age;

  employee(int id_,std::string name_,int
age_):id(id_),name(name_),age(age_){}

  friend std::ostream& operator<<(std::ostream& os,const employee& e)
  {
    os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
    return os;
  }
};

I got some errors

/home/jamaj/src/wxTestes/multiindexsample/main.cpp|31|error: ‘int
employee::id’ is private
/home/jamaj/src/wxTestes/multiindexsample/main.cpp|69|error: within
this context|

typedef multi_index_container<
  employee,
  indexed_by<
    ordered_unique<
      tag<id>, BOOST_MULTI_INDEX_MEMBER(employee,int,id)>,
    ordered_non_unique<
      tag<name>,BOOST_MULTI_INDEX_MEMBER(employee,std::string,name)>,
    ordered_non_unique<
      tag<age>, BOOST_MULTI_INDEX_MEMBER(employee,int,age)> >
> employee_set;

So, I had to change the scope of index class members to public, and it
worked:

class employee
{
  public:
  int id;
  std::string name;
  int age;

  employee(int id_,std::string name_,int
age_):id(id_),name(name_),age(age_){}

  friend std::ostream& operator<<(std::ostream& os,const employee& e)
  {
    os<<e.id<<" "<<e.name<<" "<<e.age<<std::endl;
    return os;
  }
};

The question is: is there any other way to do it without exposing all
class members?

Thanks in advance.

Best Regards.

José Augusto Jr. jamaj


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net