www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - new class howto?

reply newbie <newbie nospam.com> writes:
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
next sibling parent newbie <newbie nospam.com> writes:
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
prev sibling next sibling parent reply Frits van Bommel <fvbommel REMwOVExCAPSs.nl> writes:
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
next sibling parent newbie <newbie nospam.com> writes:
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(); } ---
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?
Jul 31 2007
prev sibling parent reply newbie <newbie nospam.com> writes:
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?
Jul 31 2007
parent reply Daniel Keep <daniel.keep.lists gmail.com> writes:
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
Jul 31 2007
parent newbee <nospam nospam.com> writes:
Daniel Keep Wrote:

 
 
 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
that was the wright thing to do - thank you all so much.
Jul 31 2007
prev sibling parent "Stewart Gordon" <smjg_1998 yahoo.com> writes:
"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 expression
On what compiler version are you getting that? Please post compiler messages in full. Stewart.
Aug 22 2007