Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I think the big problem in this is that you think you want to do one thing, when in reality you actually want to do something else. Strange, huh?</p> <blockquote> <p>Is it permissible to construct a reference to Derived from *this before the class has been initialised?</p> </blockquote> <p>Yes, as long as you don't <strong>use</strong> it (for anything but storing a reference to it) in the scope of the Base constructor and remember in ~Base that Derived is destroyed before Base.</p> <p>But why on earth do you think that Base wants to know of Derived? If it's static polymorphism you are after, then the <a href="http://en.wikipedia.org/wiki/Curiously_recurring_template_pattern" rel="nofollow">curiously recurring template pattern</a> is what you want:</p> <pre><code>template &lt;typename T&gt; class Base {}; class Derived : public Base&lt;Derived&gt; {}; </code></pre> <p>But I don't really think that's what you're aiming at.</p> <p>Maybe you want a way for Base to communicate with a client and think that should be done with inheritance? If so, then this observer-ish idiom is what you need:</p> <pre><code>class Client { public: virtual void Behavior() = 0; protected: ~Client() {} }; class Base { Client&amp; client_; public: Base(Client&amp; client) : client_(client) {} }; class Implementor : public Client { public: Implementor() : Base(*this) {} virtual void Behavior() { ... } }; </code></pre> <p>If not even that is what you want, then you need to rethink your design.</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