Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Can you not have a root Container class that contains elements:</p> <pre><code>template &lt;typename T&gt; class Container { public: // You'll likely want to use shared_ptr&lt;T&gt; instead. virtual void push(T *element) = 0; virtual T *pop() = 0; virtual void InvokeSomeMethodOnAllItems() = 0; }; template &lt;typename T&gt; class List : public Container&lt;T&gt; { 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-&gt;SomeMethod(); } } }; </code></pre> <p>These containers can then be passed around polymorphically:</p> <pre><code>class Item { public: virtual void SomeMethod() = 0; }; class ConcreteItem { public: virtual void SomeMethod() { // Do something } }; void AddItemToContainer(Container&lt;Item&gt; &amp;container, Item *item) { container.push(item); } ... List&lt;Item&gt; listInstance; AddItemToContainer(listInstance, new ConcreteItem()); listInstance.InvokeSomeMethodOnAllItems(); </code></pre> <p>This gives you the Container interface in a type-safe generic way.</p> <p>If you want to add constraints to the type of elements that can be contained, you can do something like this:</p> <pre><code>class Item { public: virtual void SomeMethod() = 0; typedef int CanBeContainedInList; }; template &lt;typename T&gt; class List : public Container&lt;T&gt; { typedef typename T::CanBeContainedInList ListGuard; // ... as before }; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload