D - Initialize class without new??
- Braden MacDonald (26/26) Apr 24 2004 Hi Everyone,
- C. Sauls (12/20) Apr 24 2004 Firstly, you have to use the 'new' operator. D class variables are
- Andy Friesen (4/15) Apr 24 2004 Can you use a struct instead of a class? D structs aren't polymorphic,
- Braden MacDonald (5/15) Apr 24 2004 Thanks for the tips; I tried this, and it works well. My converted code ...
Hi Everyone, Sorry if this is an obvious question; I'm just starting D programming. I was wondering, how do I initialize a class without using the new keyword? I am doing systems programming, so there is no malloc available, and I want the class to occupy a fixed size of the parent class. Kernel k = new Kernel(*mb_magic, mb_info);// Compiles, but doesn't link // (_d_newclass not included) Kernel k(*mb_magic, mb_info);// Compile error Kernel k = Kernel(*mb_magic, mb_info);// Compile error Kernel k; //Compiles, but I don't see how it calls my constructor // with two arguments... Basically, I don't want the garbage collector, or any dynamic memory allocation, I just want the class to be a fixed part in the binary. I want something equivalent to the C++ statement: Kernel k(*mb_magic, mb_info); Thanks! -- Braden MacDonald
Apr 24 2004
Braden MacDonald wrote:Hi Everyone, Sorry if this is an obvious question; I'm just starting D programming. I was wondering, how do I initialize a class without using the new keyword? I am doing systems programming, so there is no malloc available, and I want the class to occupy a fixed size of the parent class.Firstly, you have to use the 'new' operator. D class variables are references to an arbitrary object, not the object itself. So your Kernal k; creates a variable which can hold a referance to any Kernal object. You can write a custom allocator, and can disable the GC and/or preallocate memory to get realtime performance. Not my area, really, but I know some people have been playing with this before. Check out http://www.digitalmars.com/d/memory.html#newdelete to see about custom allocation. -C. Sauls -Invironz
Apr 24 2004
Braden MacDonald wrote:Hi Everyone, Sorry if this is an obvious question; I'm just starting D programming. I was wondering, how do I initialize a class without using the new keyword? I am doing systems programming, so there is no malloc available, and I want the class to occupy a fixed size of the parent class. I want something equivalent to the C++ statement: Kernel k(*mb_magic, mb_info); Thanks!Can you use a struct instead of a class? D structs aren't polymorphic, but they can live on the stack. -- andy
Apr 24 2004
In article <c6e9pd$1j3l$1 digitaldaemon.com>, Andy Friesen says...Braden MacDonald wrote:Thanks for the tips; I tried this, and it works well. My converted code (from C++) now runs like a charm. Thanks for your help. -- Braden... I was wondering, how do I initialize a class without using the new keyword? I am doing systems programming, so there is no malloc available, >> ... I want something equivalent to the C++ statement: Kernel k(*mb_magic, mb_info); Thanks!Can you use a struct instead of a class? D structs aren't polymorphic, but they can live on the stack. -- andy
Apr 24 2004