Initializing a class pointer

Simen kjaeraas simen.kjaras at gmail.com
Sat Feb 26 15:10:42 PST 2011


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?


-- 
Simen


More information about the Digitalmars-d-learn mailing list