Initializing a class pointer

Tyro[a.c.edwards] nospam at home.com
Sat Feb 26 15:33:23 PST 2011


On 2/27/2011 8:10 AM, Simen kjaeraas wrote:
> Tyro[a.c.edwards] <nospam at home.com> wrote:
>
>> class Class{}
>>
>> void main()
>> {
>> Class myClass;
>> Class* pClass0 = &myClass; // OK
>>
>> Class* pClass1 = new Class; // Error: cannot implicitly convert [8]
>> // expression (new Class) of type t.Class
>> // to test.Class*
>>
>> Class* pClass2 = &(new Class);
>> // Error: new Class is not an lvalue [12]
>>
>> Class mClass = &(new Class);// Error: cannot implicitly convert [14]
>> // expression (&new Class) of type Class*
>> // to test.Class
>> }
>>
>> C++ uses the process on line [8] above to initialize a class pointer.
>> Obviously it does not work in D. But given the error message at [14],
>> I thought [12] would have been allowed. What is the proper way to
>> convert [8] to D?
>
> Classes in D are already references (like Class& in C++), thus line [8]
> would be a pointer to a reference to a class, something which may make
> some kind of sense, but is unlikely to be what you want.
>
> Perhaps this question is better answered if you explain why you want a
> pointer to a class?
>
>

I'm trying to convert some c++ code that defines

T func(par...)
{	
   Controller * pCtrl = WinGetLong<Controller *> (hwnd);
   .
   .
   .
   switch(msg)
   {
   case FirstMatch:
      pCtrl = new Controller (hwnd, reinterpret_cast<CREATESTRUCT *> 
(lParam));
      break;
   }
}

I'm not sure why I need a pointer to the class, just trying to figure it 
out.


More information about the Digitalmars-d-learn mailing list