Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following is off the cuff, and not fully tested. However I find myself wondering if the well known handle/body idiom might be a good match for your requirements here. It works by making <code>Matrix</code> nothing but a pointer to an implementation (what you currently call <code>Matrix</code>). And then derived clients derive from the implementation, not from <code>Matrix</code> itself.</p> <p>Here's a small sketch:</p> <pre><code>#include &lt;memory&gt; class Matrix { public: class Imp { public: virtual ~Imp() = default; virtual auto transposed() const -&gt; std::unique_ptr&lt;Imp&gt; = 0; }; private: std::unique_ptr&lt;Imp&gt; data_; public: Matrix() = default; explicit Matrix(std::unique_ptr&lt;Imp&gt; data) : data_(std::move(data)) {} auto transposed() const -&gt; Matrix { return Matrix(data_ ? data_-&gt;transposed() : nullptr); } }; class DenseMatrix : public Matrix::Imp { public: virtual auto transposed() const -&gt; std::unique_ptr&lt;Imp&gt; { std::unique_ptr&lt;DenseMatrix&gt; res(new DenseMatrix); // Do some magic return std::move(res); } }; </code></pre> <p>It means all of your data must be on the heap. But that is probably going to happen anyway in a situation like this.</p> <p>External clients just deal with <code>Matrix</code>, which is not a base class, but a "handle" to a pointer to the base class. New implementations derive from the internal base class instead of from the <code>Matrix</code> class itself.</p> <p>It doesn't work all of the time. And I've glossed over details like how the client is going to specify he wants (e.g.) <code>DenseMatrix</code> as opposed to some other type of <code>Matrix</code>. But this general data structure can sometimes be quite useful.</p>
 

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