digitalmars.D.learn - new class howto?
- newbie (23/23) Jul 31 2007 hi,
- newbie (20/20) Jul 31 2007 sorry, it should be
- Frits van Bommel (11/21) Jul 31 2007 You can only use constant expressions as initializers for global
- newbie (9/33) Jul 31 2007 thank you for the reply.
- newbie (13/37) Jul 31 2007 it is seeminglx not possible to call d functions or initialize d classes...
- Daniel Keep (7/51) Jul 31 2007 Wait; you're calling this from WinMain?
- newbee (2/58) Jul 31 2007 that was the wright thing to do - thank you all so much.
- Stewart Gordon (7/13) Aug 22 2007 You're missing the semicolon after the module name.
hi, why is it that one can not create a class with the following: module A extern (Windows): uint P_Start(char * pParam); void P_Stop(); module A import A; class XXX { public: this() {} uint start(char *yy) { return P_Start(yy); } void start(char *yy) { P_Stop(); } } 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.
Jul 31 2007
sorry, it should be module A extern (Windows): uint P_Start(char * pParam); void P_Stop(); module B import A; class XXX { public: this() {} uint start(char *yy) { return P_Start(yy); } void start(char *yy) { P_Stop(); } } module C import B; XXX tester = new XXX();
Jul 31 2007
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(); } ---
Jul 31 2007
Frits van Bommel Wrote:newbie wrote:thank you for the reply. it will compile, but it will fail with an exception. that will also happen if i declare the variable in a function and than try to do my new. void testers() { XXX tester = new XXX(); <---- exception tester.Stop(); } could it be, that the extern(Windows) is a problem?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(); } ---
Jul 31 2007
Frits van Bommel Wrote:newbie wrote: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?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(); } ---
Jul 31 2007
newbie wrote:Frits van Bommel Wrote: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. -- Danielnewbie wrote: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?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(); } ---
Jul 31 2007
Daniel Keep Wrote:newbie wrote:that was the wright thing to do - thank you all so much.Frits van Bommel Wrote: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. -- Danielnewbie wrote: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?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(); } ---
Jul 31 2007
"newbie" <newbie nospam.com> wrote in message news:f8n64d$1bev$1 digitalmars.com...hi, why is it that one can not create a class with the following: module A extern (Windows):You're missing the semicolon after the module name. <snip>it will always generate an error during compilation: non-constant expressionOn what compiler version are you getting that? Please post compiler messages in full. Stewart.
Aug 22 2007