Is there any better approach to private inheriting from the STL containers?

Discussion in 'C++' started by Michael Tsang, Apr 27, 2011.

  1. -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    I'm writing 2 classes which is intended to be used like any other STL
    containers:

    // begin code
    class Lrv {
    public:
    Lrv();
    Lrv(const Lrv &rhs);
    explicit Lrv(int number);
    Lrv &operator=(const Lrv &trailer);
    operator bool() const;
    const int &phase;
    const int &number;
    const bool &trailer;
    bool is_compatible_with(const Lrv &trailer) const;
    bool matches(unsigned constraint) const;
    private:
    int _phase, _number;
    bool _trailer;
    };

    class Fleet : private vector<Lrv> {
    typedef vector<Lrv> Base;
    public:
    // types
    using Base::reference;
    using Base::const_reference;
    using Base::iterator;
    using Base::const_iterator;
    using Base::size_type;
    using Base::difference_type;
    using Base::value_type;
    using Base::allocator_type;
    using Base::pointer;
    using Base::const_pointer;
    using Base::reverse_iterator;
    using Base::const_reverse_iterator;
    // statics
    const static unsigned new_lrv = 1, phase_3_lrv = 2, couple = 4, renewed =
    8;
    // constructors and destructors
    Fleet();
    Fleet(const Fleet &rhs);
    // operators
    Fleet &operator=(const Fleet &rhs);
    const_reference operator[](size_type n) const;
    // other functions
    using Base::get_allocator;
    const_iterator begin() const;
    const_iterator end() const;
    const_reverse_iterator rbegin() const;
    const_reverse_iterator rend() const;
    using Base::size;
    using Base::max_size;
    using Base::capacity;
    using Base::empty;
    const_reference at(size_type n) const;
    const_reference front() const;
    const_reference back() const;
    pair<Lrv, Lrv> get(unsigned constraint);
    void put(const pair<Lrv, Lrv> &set);
    private:
    pair<Lrv, Lrv> get_internal(iterator first, iterator last);
    };

    class Timetable : private map<int, pair<Lrv, Lrv> > {
    typedef map<int, pair<Lrv, Lrv> > Base;
    public:
    // types
    using Base::key_type;
    using Base::mapped_type;
    using Base::value_type;
    using Base::key_compare;
    using Base::allocator_type;
    using Base::reference;
    using Base::const_reference;
    using Base::iterator;
    using Base::const_iterator;
    using Base::size_type;
    using Base::difference_type;
    using Base::pointer;
    using Base::const_pointer;
    using Base::reverse_iterator;
    using Base::const_reverse_iterator;
    using Base::value_compare;
    // constructors and destructors
    Timetable();
    Timetable(const Timetable &rhs);
    explicit Timetable(int weekday);
    template<class Iterator> Timetable(Iterator first, Iterator last);
    template<class Iterator>
    Timetable(int weekday, Iterator first, Iterator last);
    // operators
    Timetable &operator=(const Timetable &rhs);
    using Base::eek:perator[];
    // other functions;
    using Base::get_allocator;
    using Base::begin;
    using Base::end;
    using Base::rbegin;
    using Base::rend;
    using Base::empty;
    using Base::size;
    using Base::max_size;
    using Base::insert;
    using Base::erase;
    using Base::swap;
    using Base::clear;
    using Base::key_comp;
    using Base::value_comp;
    using Base::find;
    using Base::count;
    using Base::lower_bound;
    using Base::upper_bound;
    using Base::equal_range;
    void allocate(Fleet &fleet);
    private:
    enum {depot_single = -1, depot_couple = -2};
    map<int, unsigned> constraints;
    list<pair<int, int> >reforms;
    };

    bool operator==(const Fleet &lhs, const Fleet &rhs);
    bool operator!=(const Fleet &lhs, const Fleet &rhs);
    bool operator==(const Timetable &lhs, const Timetable &rhs);
    bool operator!=(const Timetable &lhs, const Timetable &rhs);
    bool operator<(const Timetable &lhs, const Timetable &rhs);
    bool operator<=(const Timetable &lhs, const Timetable &rhs);
    bool operator>(const Timetable &lhs, const Timetable &rhs);
    bool operator>=(const Timetable &lhs, const Timetable &rhs);
    void swap(Timetable &a, Timetable &b);

    // end code

    I don't want any automatic conversion from these classes to the STL base
    classes because the bases are not virtual, however, I want them to behave
    identically to STL classes. (The Fleet class should be a read-only container
    that can only be modified by the member functions get and put; the Timetable
    class should be read/writeable.) If I use composition instead, I need to get
    through the headaches wrapping every public member function from the STL
    classes. As the classes represent real-life objects, I also don't want to
    write global functions operating on these classes (as the constructors are
    very complex)



    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.11 (GNU/Linux)

    iEYEARECAAYFAk23zIgACgkQZ1Turg5KUCmqaACfdSr45IlriGpvoy9qFUM+W15l
    iyQAn11dNIV5ovifbFshoul50kizfcUQ
    =Ro/c
    -----END PGP SIGNATURE-----
     
    Michael Tsang, Apr 27, 2011
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Hitesh Joshi
    Replies:
    1
    Views:
    271
    Sybren Stuvel
    Jul 19, 2006
  2. Replies:
    7
    Views:
    578
    Pete Becker
    Jan 25, 2008
  3. Peng Yu
    Replies:
    4
    Views:
    301
    Kai-Uwe Bux
    Sep 14, 2008
  4. Andrey Vul
    Replies:
    6
    Views:
    590
    James Kanze
    Oct 22, 2009
  5. Sebastian Mach
    Replies:
    5
    Views:
    342
Loading...

Share This Page