Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 929107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T20:06:55+00:00 2026-05-15T20:06:55+00:00

I have different types, say A , B , C , that all inherit

  • 0

I have different types, say A, B, C, that all inherit from some base class Base:

class Base { ... };
class A : public Base { ... };
class B : public Base { ... };
class C : public Base { ... };

I need a container, let’s call it Master, that holds pointers to objects of types A, B and C. I want the Master container to provide an iterator over all contained Base objects, as well as specifically-typed iterators over all contained A, B and C objects. As a storage backend, I’ll be using std::vector, but it would be nice if this can be switched easily later on.

Conceptually, this is the interface that Master should present to the outside world:

class Master {

  public:

    add(A *a);
    add(B *b);
    add(C *c);

    remove(Base *base);

    iterator<A*> a_begin();
    iterator<A*> a_end();
    iterator<B*> b_begin();
    iterator<B*> b_end();
    iterator<C*> c_begin();
    iterator<C*> c_end();
    iterator<Base*> base_begin();
    iterator<Base*> base_end();
    // also: reverse iterators, const iterators, reverse const iterators
};

The interface does not have to match this precise syntax. For example, someMaster.begin<A>() is perfectly fine too.

The trouble is, even in this simplified interface, you can already see some code duplication happening. It’s much worse in the implementation. This is unacceptable, because I want to be able to extend the Master container easily later on, if I want to add classes D, E and F (also inheriting from Base). Preferably, I would like to extend it with just one or two lines of code.

All this could be implemented with lots of dynamic_casting, but that’s ugly. I think some magic with templates and multiple inheritance could help me out here. What would be the cleanest implementation of this class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T20:06:56+00:00Added an answer on May 15, 2026 at 8:06 pm

    Here’s a sketch of what I would do:

    // Beware, brain-compiled code ahead
    template< typename T >
    class typed_container
    {
      typedef std::vector<T> data_t;
    public:
      typedef data_t::iterator iterator;
      iterator begin() {return data_.begin();}
      iterator end  () {return data_.end();}
    private:
      data_t data_;
    };
    
    typedef my_type_list<A,B,C> types_t;
    
    class master : public derive_from< typed_container, types_t > {
      template< typename T >
      struct traits {
        typedef typename typed_container<T>::iterator iterator;
        iterator begin(typed_container<T>& c) {return c.begin();}
        iterator end  (typed_container<T>& c) {return c.end  ();}
      };
    public:
      template< typename T > 
      typename traits<T>::iterator begin() {return traits<T>::begin(*this);}
      template< typename T > 
      typename traits<T>::iterator end  () {return traits<T>::end  (*this);}
    
      typedef my_assembling_iterator<types_t> iterator;
    
      iterator begin() {return my_assembling_iterator<types_t>.begin(*this);}
      iterator end  () {return my_assembling_iterator<types_t>.end  (*this);}
    };
    

    That leaves you to implement my_type_list (rather simple), derive_from (not as simple, but not too hard either), and my_assembling_iterator (I hadn’t had a need to do something like that yet).


    You can find a working C++03 type list implementation here. It only takes up to nine template arguments (but that’s easily extended), and you’ll have to write

    typedef my_type_list<A,B,C>::result_t types_t
    

    but it’s simple and free and I know it works (because I’m using this library myself).

    The derive_from template would look something like this:

    //Beware, brain-compiled code ahead!
    template< template<typename> class C, class  >
    struct derive_from;
    
    template< template<typename> class C >
    struct derive_from< C, nil > {};
    
    template< template<typename> class C, typename Head, typename Tail >
    struct derive_from< C, my_type_list<Head,Tail> > : public C<Head>
                                                     , public derive_from<C,Tail> {};
    

    That leaves the iterator. What are your needs regarding it? Does it have to be a random-access iterator (hard) or would a forward iterator suffice? Do you need any particular order to iterate over the elements?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble with a certain mapping. Let's say I have three assessments that
I have an issue where I have an interface that has parts that make
I have a series of classes A , B , ... which have many
I'm a perl newbie. I have a code in which a variable is loaded
I have about 60 images uploaded to my site. I'd like to resize them
First of all, sorry if this might be a stupid question. I'm very new
Any help with building the following design around a .NET app would be appreciated.
This post is actually divided in two questions: Which kinds of associations classes/interfaces usually
I am looking for a good naming convention for methods (and variables to a
I'm torn how to structure my code to accommodate business rules specific to instances

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.