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

  • SEARCH
  • Home
  • 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 57597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:42:58+00:00 2026-05-10T17:42:58+00:00

It looks like I had a fundamental misunderstanding about C++ :< I like the

  • 0

It looks like I had a fundamental misunderstanding about C++ :<

I like the polymorphic container solution. Thank you SO, for bringing that to my attention 🙂


So, we have a need to create a relatively generic container type object. It also happens to encapsulate some business related logic. However, we need to store essentially arbitrary data in this container – everything from primitive data types to complex classes.

Thus, one would immediately jump to the idea of a template class and be done with it. However, I have noticed C++ polymorphism and templates do not play well together. Being that there is some complex logic that we are going to have to work, I would rather just stick with either templates OR polymorphism, and not try to fight C++ by making it do both.

Finally, given that I want to do one or the other, I would prefer polymorphism. I find it much easier to represent constraints like ‘this container contains Comparable types’ – a la java.

Bringing me to the topic of question: At the most abstract, I imagine that I could have a ‘Container’ pure virtual interface that has something akin to ‘push(void* data) and pop(void* data)’ (for the record, I am not actually trying to implement a stack).

However, I don’t really like void* at the top level, not to mention the signature is going to change every time I want to add a constraint to the type of data a concrete container can work with.

Summarizing: We have relatively complex containers that have various ways to retrieve elements. We want to be able to vary the constraints on the elements that can go into the containers. Elements should work with multiple kinds of containers (so long as they meet the constraints of that particular container).

Edit: I should also mention that the containers themselves need to be polymorphic. That is my primary reason for not wanting to use templated C++.

So – should I drop my love for Java type interfaces and go with templates? Should I use void* and statically cast everything? Or should I go with an empty class definition ‘Element’ that declares nothing and use that as my top level class in the ‘Element’ hierarchy?

One of the reasons why I love stack overflow is that many of the responses provide some interesting insight on other approaches that I hadn’t not have even considered. So thank you in advance for your insights and comments.

  • 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. 2026-05-10T17:42:58+00:00Added an answer on May 10, 2026 at 5:42 pm

    Can you not have a root Container class that contains elements:

    template <typename T> class Container { public:      // You'll likely want to use shared_ptr<T> instead.    virtual void push(T *element) = 0;    virtual T *pop() = 0;    virtual void InvokeSomeMethodOnAllItems() = 0; };  template <typename T> class List : public Container<T> {     iterator begin();     iterator end(); public:     virtual void push(T *element) {...}     virtual T* pop() { ... }     virtual void InvokeSomeMethodOnAllItems()      {        for(iterator currItem = begin(); currItem != end(); ++currItem)        {            T* item = *currItem;            item->SomeMethod();        }     } }; 

    These containers can then be passed around polymorphically:

    class Item { public:    virtual void SomeMethod() = 0; };  class ConcreteItem { public:     virtual void SomeMethod()      {         // Do something     } };    void AddItemToContainer(Container<Item> &container, Item *item) {    container.push(item); }  ...  List<Item> listInstance; AddItemToContainer(listInstance, new ConcreteItem()); listInstance.InvokeSomeMethodOnAllItems(); 

    This gives you the Container interface in a type-safe generic way.

    If you want to add constraints to the type of elements that can be contained, you can do something like this:

    class Item { public:   virtual void SomeMethod() = 0;   typedef int CanBeContainedInList; };  template <typename T> class List : public Container<T> {    typedef typename T::CanBeContainedInList ListGuard;    // ... as before }; 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Looks like other people have had this issue, but I would like to understand
I have something that looks like this H1 H2 CHK1 N1 D1 CHK2 N2
Supposing you had a data model that looks like: /-----------------------\ /-----------------------\ | Patient |
It looks like I have some problem with this code. I need to pass
I have code that looks like this.... function Finder(id) { this.id = id; this.input
I have a query that looks like the following: SELECT * FROM table WHERE
It looks like people have had issues with Accept headers in the past, but
Ok, so I had an IBOutlet of a UIImageView that looks like this: .h
I have array that i had to unset some indexes so now it looks
Looks like others have had this problem but I can't seem to find a

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.