Cannot cast pointer field while can cast same pointer defined within method in managed classes
<p>I have unmanaged object of WtfClass.</p>

<pre><code>class WtfClass    {    };
</code></pre>

<p>And I also have managed class which uses pointer to this object.</p>

<pre><code>ref class MyClass  //works fine if you remove "ref"
{
public:
    void MyMethod();
    void WtfMethod(void * pVoid);
    WtfClass *pWtfStruct;
};

void MyClass::MyMethod()
{
    /*WtfClass* pWtfStruct;  //if you uncomment this it will compile even with ref*/
    WtfMethod((int*)(&amp;pWtfStruct));  //(!!!invalid type conversion here)
}

void MyClass::WtfMethod(void *pVoid)
{}
</code></pre>

<p>I can't cast WtfClass* pointer from field, but can easily cast the same pointer defined within MyMethod(). If make MyClass unmanaged it works in any case. </p>

<p>It's better to look at screenshots:</p>

<ol>
<li><a href="https://ibin.co/2iOcN1ooaC7A.png" rel="nofollow">https://ibin.co/2iOcN1ooaC7A.png</a> <strong>[using ref-bad.png]</strong></li>
<li><a href="https://ibin.co/2iOcYtP84H0e.png" rel="nofollow">https://ibin.co/2iOcYtP84H0e.png</a> <strong>[using ref-good.png]</strong></li>
<li>ibin.co/2iOcjCCc2gQe.png <strong>[without ref.png]</strong> (sorry not enough reputation to paste more than 2 links)</li>
</ol>

<p>Of course I can have workaround like this, but I'd like to understand why this happening:</p>

<pre><code>void MyClass::MyMethod()
{
    WtfClass* pWorkAround = pWtfStruct;  //not required in this case
    WtfMethod((void*)(&amp;pWorkAround));
}
</code></pre>