讓您擁有一個類別的僱員模板類和C中的關鍵字friend ++(具體的例子指的boost :: multi_index)
class employee {
public:
employee(const string &name, int id) : m_name(name) , m_id(id) {}
const string &getName() const { return m_name; }
int getID() const { return m_id; }
private:
string &m_name;
int m_id;
};
,你必須爲封裝私有數據成員。但現在你想用一個boost :: multi_index ....
typedef multi_index_container <
employee,
indexed_by<
ordered_non_unique<
composite_key<
Name,
member< employee, string & , &employee::m_name>,
member< employee, int, &employee::m_id>
>
>
>
> employee_set;
所以我可以使用BOOST_INDEX_CONST_MEM_FUN ...
typedef multi_index_container <
employee,
indexed_by<
ordered_non_unique<
composite_key<
Name,
BOOST_MULTI_INDEX_CONST_MEM_FUN(employee, const string&, getName),
BOOST_MULTI_INDEX_CONST_MEM_FUN(employee, int, getID)
>
>
>
> employee_set;
但我真的很想做的是給予employee_set的訪問我的員工類的私人數據成員。我只是不知道該怎麼做: -/
指向成員的指針,而不是從它產生的指針,需要存儲並在multi_index模板中傳遞。 – Potatoswatter 2010-08-15 09:41:52
並非如此,該特定模板參數是非類型參數,因此它嵌入成員<>模板實例化的類型中。 – SCFrench 2010-08-15 11:52:59
或者您可以直接在員工中提供employee_set typedef – 2010-08-17 08:57:27