Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It is possible to create an inheritance tree for containers, reflecting the inheritance tree of the data. If you have the following data:</p> <pre><code>class Interface { public: virtual ~Interface() {} virtual void print() = 0; }; class Number : public Interface { public: Number(int value) : x( value ) {} int get() const { return x; } void print() { std::printf( "%d\n", get() ); }; private: int x; }; class String : public Interface { public: String(const std::string &amp; value) : x( value ) {} const std::string &amp;get() const { return x; } void print() { std::printf( "%s\n", get().c_str() ); } private: std::string x; }; </code></pre> <p>You could also have the following containers:</p> <pre><code>class GenericContainer { public: GenericContainer() {} ~GenericContainer() { v.clear(); } virtual void add(Interface &amp;obj) { v.push_back( &amp;obj ); } Interface &amp;get(unsigned int i) { return *v[ i ]; } unsigned int size() const { return v.size(); } private: std::vector&lt;Interface *&gt; v; }; class NumericContainer : public GenericContainer { public: virtual void add(Number &amp;obj) { GenericContainer::add( obj ); } Number &amp;get(unsigned int i) { return (Number &amp;) GenericContainer::get( i ); } }; class TextContainer : public GenericContainer { public: virtual void add(String &amp;obj) { GenericContainer::add( obj ); } String &amp;get(unsigned int i) { return (String &amp;) GenericContainer::get( i ); } }; </code></pre> <p>This is not the best performing code; it is just to give an idea. The only problem with this approach is that every time you add a new Data class, you have to also create a new Container. Apart from that, you have polymorphism "working again". You can be specific or general:</p> <pre><code>void print(GenericContainer &amp; x) { for(unsigned int i = 0; i &lt; x.size(); ++i) { x.get( i ).print(); } } void printNumbers(NumericContainer &amp; x) { for(unsigned int i = 0; i &lt; x.size(); ++i) { printf( "Number: " ); x.get( i ).print(); } } int main() { TextContainer strContainer; NumericContainer numContainer; Number n( 345 ); String s( "Hello" ); numContainer.add( n ); strContainer.add( s ); print( strContainer ); print( numContainer ); printNumbers( numContainer ); } </code></pre>
    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.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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