www.digitalmars.com         C & C++   DMDScript  

D - Initialize class without new??

reply Braden MacDonald <bradenm_k shaw.ca> writes:
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
next sibling parent "C. Sauls" <ibisbasenji yahoo.com> writes:
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
prev sibling parent reply Andy Friesen <andy ikagames.com> writes:
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
parent Braden MacDonald <bradenm_k shaw.ca> writes:
In article <c6e9pd$1j3l$1 digitaldaemon.com>, Andy Friesen says...
Braden MacDonald wrote:
 ...
 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

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
Apr 24 2004