Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Inheritance in C++/CX is <strong>not</strong> the same as inheritance in C++. This is because C++/CX ref classes are in fact COM objects, which implements inheritance through a variety of aggregation.</p> <p>The example I was working with was</p> <pre><code>public ref class MyTextBox : public TextBox { MyTextBox() {}; ~MyTextBox() {}; virtual void OnKeyDown(KeyEventArgs^ e) override { TextBox::OnKeyDown(e); }; }; </code></pre> <p>which won't work, because TextBox::OnKeyDown() will effectively call MyTextBox::OnKeyDown(). This is because of how virtual methods are implemented in C++/CX and COM, an in-depth overview can be read <a href="http://www.interact-sw.co.uk/iangblog/2011/09/25/native-winrt-inheritance" rel="nofollow">here</a>. </p> <p><strong>Visual Studio 11 Developer Preview Solution</strong></p> <p>The short version is that the OnKeyDown() method is part of the IControlOverrides interface, implemented by both MyTextBox and TextBox, thanks to some clever compiler tricks. To get hold of the interface pointer for the TextBox implementation, we first need to ask the TextBox for it - if we ask MyTextBox we end up where we started. And because this is inheritance in COM, not C++, we do that through a pointer to the <em>baseclass</em> object instead of <em>this</em>:</p> <pre><code> virtual void OnKeyDown(KeyEventArgs^ e) override { struct IControlOverrides^ ico; HRESULT hr = __cli_baseclass-&gt;__cli_QueryInterface(const_cast&lt;class Platform::Guid%&gt;(reinterpret_cast&lt;const class Platform::Guid%&gt;(__uuidof(struct IControlOverrides^))),reinterpret_cast&lt;void**&gt;(&amp;ico)); if (!hr) { hr = ico-&gt;__cli_OnKeyDown(e); }; }; </code></pre> <p><strong>Visual Studio 11 Beta Solution</strong></p> <p>As with so many other things, this has been improved in the VS 11 Beta. While "__super::" still won't work, it now works fine just to make the call explicitly to the interface in which it is defined: </p> <pre><code> virtual void OnKeyDown(KeyEventArgs^ e) override { IControlOverrides::OnKeyDown(e); }; </code></pre> <p>The Object Browser in VS will show you which interface the OnKeyDown() method is defined in (IControlOverrides).</p> <p>Problem solved!</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