c++ 中的模板类和friend关键字(具体例子参考boost::multi_index)

发布于 2024-09-14 20:39:49 字数 1277 浏览 5 评论 0原文

所以你有一个类员工

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 对我的员工类的私有数据成员的访问权限。我只是不知道该怎么做:-/

so you have a class employee

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;
};

and you have private data members for encapsulation. But now you want to use a 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;

so I could use 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;

but what I'd really like to do is to grant employee_set access to my employee class's private data members. I just can't figure out how to do it :-/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

意犹 2024-09-21 20:39:49

如果我理解正确的话,它实际上是对成员指针值 &employee::m_name 的访问,用作 member<> 的第三个模板参数。在employee_set typedef 中,这会导致访问冲突(即编译器错误)。如果可以获得该指针值,那么它将在 Boost.MultiIndex 代码中传递,并且可以从任何地方调用,并且不需要进行访问检查。现在,我不知道类员工如何能够与顶级 typedef 建立友谊(我认为您只能向类和函数授予友谊),但是您可以使employee_set成为新类的公共成员,并授予友谊到那个班级:

class employee { 
  ...
  friend struct mytypedefs;
};

struct mytypedefs {
  typedef multi_index_container <
  ... &employee::m_name ...
  > employee_set;
};

int main() {
  mytypedefs::employee_set my_employee_set;
}

If I'm understanding this correctly, it is actually the access to the pointer-to-member value &employee::m_name, used as the third template parameter to member<> within the employee_set typedef, that causes the access violation (i.e. compiler error). If that pointer value could be obtained, it would then be passed around within the Boost.MultiIndex code and could be invoked from wherever, and no access checks would need to be made. Now, I don't know how class employee could grand friendship to a top-level typedef (I think you can only grant friendship to classes and functions), but you could make the employee_set a public member of a new class, and grant friendship to that class:

class employee { 
  ...
  friend struct mytypedefs;
};

struct mytypedefs {
  typedef multi_index_container <
  ... &employee::m_name ...
  > employee_set;
};

int main() {
  mytypedefs::employee_set my_employee_set;
}
秋叶绚丽 2024-09-21 20:39:49

忘记它吧。 Boost 可以选择从对象内部的任何位置访问您的对象,并且友谊永远不会传递,因此无法通过外部结构与内部结构建立友谊。

您可以尝试将这些数据成员放入私有基类中。然后,“特权”操作是从 employee 中提取基数,这可以在您自己的代码中实例化 mutli_index_container 时完成,无需 friend

或者,将它们全部设为公开

Forget about it. Boost can choose to access your object from anywhere in its internals, and friendship is never transitive, so there is no way to befriend an internal structure via an external one.

You might try putting those data members in a private base class. Then the "privileged" operation is extracting the base from employee, which can be done at the point of instantiation of mutli_index_container within your own code, no friends needed.

Or, just make them all public.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文