new class howto?

Daniel Keep daniel.keep.lists at gmail.com
Tue Jul 31 06:43:05 PDT 2007



newbie wrote:
> Frits van Bommel Wrote:
> 
>> newbie wrote:
>>> module C
>>>
>>>  XXX tester = new XXX();
>>>
>>>
>>> it will always generate an error during compilation:
>>>
>>>       non-constant expression
>>>
>>> when i try to do that in a function, then i will get an exception.
>> You can only use constant expressions as initializers for global 
>> variables, so dynamic memory allocation is disallowed. To do what you 
>> want, use:
>> ---
>> module C;
>>
>> XXX tester;
>>
>> static this() {
>>      tester = new XXX();
>> }
>> ---
> it is seeminglx not possible to call d functions or initialize d classes from a function that is decorated with
> extern(Windows)
> 
> extern (Windows) int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
> 	testery();
>     DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG_MAIN), null, &Dialog_Main);
>     return 0;
> }
> 
> 
> void testery() {
> 	  XXX testerx = new XXX;
> 	  testerx.Stop();
> }
> 
> 
> how does one call a D function or do a XXX testerx = new XXX; in such a function?
> 
> 

Wait; you're calling this from WinMain?

The code you just supplied will not work because you haven't initialised
the garbage collector, nor run module ctors, nor run unittests.  See
http://digitalmars.com/d/windows.html for an example of what your
WinMain should look like.

	-- Daniel


More information about the Digitalmars-d-learn mailing list