Note that there are some explanatory texts on larger screens.

plurals
  1. POPolicy-based template design: How to access certain policies of the class?
    primarykey
    data
    text
    <p>I have a class that uses several policies that are templated. It is called <code>Dish</code> in the following example. I store many of these <code>Dish</code>es in a <code>vector</code> (using a pointer to simple base class), but then I'd like to extract and use them. But I don't know their exact types. </p> <p>Here is the code; it's a bit long, but really simple:</p> <pre><code>#include &lt;iostream&gt; #include &lt;vector&gt; struct DishBase { int id; DishBase(int i) : id(i) {} }; std::ostream&amp; operator&lt;&lt;(std::ostream&amp; out, const DishBase&amp; d) { out &lt;&lt; d.id; return out; } // Policy-based class: template&lt;class Appetizer, class Main, class Dessert&gt; class Dish : public DishBase { Appetizer appetizer_; Main main_; Dessert dessert_; public: Dish(int id) : DishBase(id) {} const Appetizer&amp; get_appetizer() { return appetizer_; } const Main&amp; get_main() { return main_; } const Dessert&amp; get_dessert() { return dessert_; } }; struct Storage { typedef DishBase* value_type; typedef std::vector&lt;value_type&gt; Container; typedef Container::const_iterator const_iterator; Container container; Storage() { container.push_back(new Dish&lt;int,double,float&gt;(0)); container.push_back(new Dish&lt;double,int,double&gt;(1)); container.push_back(new Dish&lt;int,int,int&gt;(2)); } ~Storage() { // delete objects } const_iterator begin() { return container.begin(); } const_iterator end() { return container.end(); } }; int main() { Storage s; for(Storage::const_iterator it = s.begin(); it != s.end(); ++it){ std::cout &lt;&lt; **it &lt;&lt; std::endl; std::cout &lt;&lt; "Dessert: " &lt;&lt; *it-&gt;get_dessert() &lt;&lt; std::endl; // ?? } return 0; } </code></pre> <p>The tricky part is here, in the <code>main()</code> function:</p> <pre><code> std::cout &lt;&lt; "Dessert: " &lt;&lt; *it-&gt;get_dessert() &lt;&lt; std::endl; // ?? </code></pre> <p>How can I access the dessert? I don't even know the Dessert type (it is templated), let alone the complete type of the object that I'm getting from the storage.</p> <p>This is just a toy example, but I think my code reduces to this. I'd just like to pass those <code>Dish</code> classes around, and different parts of the code will access different parts of it (in the example: its appetizer, main dish, or dessert).</p>
    singulars
    1. This table or related slice is empty.
    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. 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