Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>This doesn't directly answer the interviewer's question, but another possible way manipulating the inheritance of A and B is to do something like this:</p> <pre><code>#include &lt;iostream&gt; #include &lt;memory&gt; #include &lt;vector&gt; using namespace std; //This concept was taken from a Going Native 2013 talk called "C++ Seasoning" given by Sean Parent // //Located here: (about 101 minutes into it) //http://channel9.msdn.com/Events/GoingNative/2013/Cpp-Seasoning //Polymorphism without inheritance. //Permits polymorphism without using pointers or references, //and allows them to be copied around easier (each instance is actually its own object) rather //than accidentally shallow-copying when you wanted deep-copies. // //Every time Object::Print() is called, it calls // Object::PrintableConcept::Print(), which virtually calls // Object::PrintableModel&lt;TYPE&gt;::Print(), which calls your // "derived" class that implements the Print() function, regardless // of what that class inherits (if anything). class Object //Class without inheritance or virtual. { public: template&lt;typename Type&gt; Object(Type instance) : self(std::make_shared&lt;PrintableModel&lt;Type&gt;&gt;(std::move(instance))) { } //Calls the "inherited" function. void Print() const { self-&gt;Print(); } private: struct PrintableConcept //The concept we want to polymorphably access. { virtual ~PrintableConcept() = default; virtual void Print() const = 0; }; //The class that concretely models the concept, //and does the actual inheritting. template&lt;typename Type&gt; struct PrintableModel : public PrintableConcept { PrintableModel(Type instance) : data(std::move(instance)) { } //Every time void Print() const override { this-&gt;data.Print(); } Type data; }; //This should be a unique_ptr, but you also need to make sure //you implement proper copy operators in this class and move support. std::shared_ptr&lt;PrintableConcept&gt; self; }; class Whatever { public: void Print() const { std::cout &lt;&lt; "Whatever\n" &lt;&lt; std::endl; } }; class SomethingElse { public: void Print() const { std::cout &lt;&lt; "SomethingElse\n" &lt;&lt; std::endl; } }; class WidgetThing { public: void Print() const { std::cout &lt;&lt; "WidgetThing\n" &lt;&lt; std::endl; } }; typedef std::vector&lt;Object&gt; Document; int main() { Document document; document.emplace_back(Whatever()); document.emplace_back(SomethingElse()); document.emplace_back(WidgetThing()); for(const auto &amp;object : document) { object.Print(); } return 0; } </code></pre> <p>&lt;&lt;&lt; <a href="http://ideone.com/R5xEs4" rel="nofollow">Run the code</a> >>></p> <p>None of the classes actually inherit from 'Object' (or from anything else), but can be used interchangeably with each other in, say, a vector, because they all implement a common interface (<code>PrintableConcept</code>) that Object accesses templatedly, but Object itself isn't a template and so doesn't become <code>Object&lt;something&gt;</code> and <code>Object&lt;something-else&gt;</code> which would've been separate types.</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.
    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