digitalmars.D.learn - new class howto?
- newbie <newbie nospam.com> Jul 31 2007
- newbie <newbie nospam.com> Jul 31 2007
- Frits van Bommel <fvbommel REMwOVExCAPSs.nl> Jul 31 2007
- newbie <newbie nospam.com> Jul 31 2007
- newbie <newbie nospam.com> Jul 31 2007
- Daniel Keep <daniel.keep.lists gmail.com> Jul 31 2007
- newbee <nospam nospam.com> Jul 31 2007
- "Stewart Gordon" <smjg_1998 yahoo.com> Aug 22 2007
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: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
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(); } ---
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
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.
variables, so dynamic memory allocation is disallowed. To do what you want, use: --- module C; XXX tester; static this() { tester = new XXX(); } ---
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
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.
variables, so dynamic memory allocation is disallowed. To do what you want, use: --- module C; XXX tester; static this() { tester = new XXX(); } ---
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
"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









newbie <newbie nospam.com> 