Give access to encapsulated container

后端 未结 6 1233
借酒劲吻你
借酒劲吻你 2021-01-13 02:38
class X {
  public:
    typedef std::list Container;

    // (1)
    const Container& GetElements() const;

    // (2)
    Container::iterator Element         


        
6条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-13 03:40

    I can't think of much cleaner methods; you might consider the lightweight (4) solution giving access with

    const Container& container() const { return m_container; }
    

    I would prefer (3) since the container type becomes fully encapsulated, i.e. your type does not necessarily require inclusion of and you can change the container type without recompiling depending modules.

提交回复
热议问题