www.digitalmars.com         C & C++   DMDScript  

D - Another few questions about D

reply Stephan Wienczny <wienczny web.de> writes:
Hallo NG,

[1] How are classes created?
     I know I've got to use the new operator:
     e.g.:
     MyClass name = new MyClass;
     	    ^
     This looks like a reference to the created class.
     In C++ a new returns a pointer, what happens here in D?

[2] What exactly does a garbage collector do?
     Does it delete a allocated piece of memory if no reference to it
     exists?

[3] I'm trying to write a linked list, because I want to learn to use D.
     You might have seen some code in an earlier post.
     So I've got to work with pointer to classes.
     I did not find anythink in the docs about it. Is it possible?

Thanks for your help

Cu Stephan
Apr 03 2003
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Stephan Wienczny wrote:

 Hallo NG,

 [1] How are classes created?
      I know I've got to use the new operator:
      e.g.:
      MyClass name = new MyClass;
             ^
      This looks like a reference to the created class.
      In C++ a new returns a pointer, what happens here in D?

Yes, classes are always handled by reference. I D, pointers-to-classes are not allowed, although pointers-to-struct are.
 [2] What exactly does a garbage collector do?
      Does it delete a allocated piece of memory if no reference to it
      exists?

It should run the destructor for the object (if any). The memory is cleaned up at a (I believe) undefined later time.
 [3] I'm trying to write a linked list, because I want to learn to use D.
      You might have seen some code in an earlier post.
      So I've got to work with pointer to classes.
      I did not find anythink in the docs about it. Is it possible?

Use references to classes just like you would a pointer: class Foo { public; Foo next; }; Foo a = new Foo; a.next = new Foo; -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
Apr 03 2003
parent reply "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3E8C8929.DDF10821 deming-os.org...
 Stephan Wienczny wrote:

 Hallo NG,

 [1] How are classes created?
      I know I've got to use the new operator:
      e.g.:
      MyClass name = new MyClass;
             ^
      This looks like a reference to the created class.
      In C++ a new returns a pointer, what happens here in D?

Yes, classes are always handled by reference. I D, pointers-to-classes

 not allowed, although pointers-to-struct are.

WRONG ... its just a ptr to class is basically a ** simple demo example using the ** behaviour for a singly linked list. and I have run and tested it. class Foo { Foo next; int val; this( int i ) { val = i; } } Foo head; void addFoo( int i ) { Foo tmp = new Foo( i ); tmp.next = head; head = tmp; } void findFoo( int i ) { int count; Foo * headPtr; count = 0; headPtr = &head; while( (*headPtr) !== null ) { count++; if ( (*headPtr).val == i ) { printf( "found %d at %d\n", i, count ); return; } headPtr = &((*headPtr).next); } printf( "- %d not found in %d items\n", i, count ); } int main( char[][] args ) { findFoo( 1 ); addFoo( 1 ); addFoo( 2 ); addFoo( 3 ); // foo list should now be 3, 2, 1 findFoo( 1 ); findFoo( 5 ); addFoo( 4 ); addFoo( 5 ); // foo list should now be 5, 4, 3, 2, 1 findFoo( 1 ); findFoo( 5 ); findFoo( 6 ); return 0; }
 [2] What exactly does a garbage collector do?
      Does it delete a allocated piece of memory if no reference to it
      exists?

It should run the destructor for the object (if any). The memory is

 up at a (I believe) undefined later time.

 [3] I'm trying to write a linked list, because I want to learn to use D.
      You might have seen some code in an earlier post.
      So I've got to work with pointer to classes.
      I did not find anythink in the docs about it. Is it possible?

Use references to classes just like you would a pointer: class Foo { public; Foo next; }; Foo a = new Foo; a.next = new Foo; -- The Villagers are Online! villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]

Apr 03 2003
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Mike Wynn wrote:

 Yes, classes are always handled by reference.  I D, pointers-to-classes

 not allowed, although pointers-to-struct are.

WRONG ... its just a ptr to class is basically a **

Are you arguing that a pointer to a reference is allowed? Yes, I assumed that would be the case, since you generally can take pointer-to-anything in C derivatives. Thus, D doesn't have pointer-to-class. It has pointer-to-reference-to-class. As was implicit in my note, pointer-to-reference-to-class is roughly equivalent to pointer-to-pointer-to-class. Frankly, your response was kind of unpleasant. -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
Apr 03 2003
next sibling parent "Mike Wynn" <mike.wynn l8night.co.uk> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3E8CC8DB.C16A46CD deming-os.org...
 Mike Wynn wrote:

 Yes, classes are always handled by reference.  I D,



 are
 not allowed, although pointers-to-struct are.

WRONG ... its just a ptr to class is basically a **

Are you arguing that a pointer to a reference is allowed? Yes, I assumed

 would be the case, since you generally can take pointer-to-anything in C
 derivatives.

 Thus, D doesn't have pointer-to-class.  It has

 As was implicit in my note, pointer-to-reference-to-class is roughly

 to pointer-to-pointer-to-class.

D does not allow Classes (afaik) in the C++ sense, they are only ever references, there is no way to create an static array of instances `Foo[2] myFoos` is 2 references to instances. so pointers (and assoc arithmetic are of no value). I took your pointers-to-class to mean that you thought that your could not take the address of a class (D sense). my mistake. had you said no pointer to instance I would have realised quicker. D objects are passed by reference BUT like Java they are more akin to a pointer than a Reference in the C++ sense D: Foo func( Foo a, Foo b ) { if ( otherFunc( a, b ) ) a = b; // a now refers to b original instance a refered to is uneffected return a; } D is more like Foo * func( Foo * a, Foo * b ) { // almost `Foo * const ` but not can be reassigned just not moved. if ( otherFunc( a, b ) ) a = b; // operator = return a; } there is no D equiv to the C++ Foo & func( Foo & a, Foo & b ) { if ( otherFunc( a, b ) ) a = b; // operator = return a; } or Foo[10] block_of_foo;
 Frankly, your response was kind of unpleasant.

Apr 03 2003
prev sibling parent Ilya Minkov <midiclub 8ung.at> writes:
Russ Lewis wrote:
 Thus, D doesn't have pointer-to-class.  It has pointer-to-reference-to-class.
 As was implicit in my note, pointer-to-reference-to-class is roughly equivalent
 to pointer-to-pointer-to-class.

Not a "reference to class", but simply a "class", since in D as well as in Delphi an object (class) is *defined* as a reference to a *structure* of *unknown size*. This last circumstance is actually very important. Do you remember making a structure of flexible length? You simply define a structure whose last element is a fixed-length array. Then, you allocate as much space for it as you need and handle it through a pointer. Obviously, you cannot handle such a structure directly since whenever you try to pass it to another function, it would not survive it well. Furthermore, you'd have no way to allocate it in the first place. :) The reason is, that a value of unknown size cannot be passed into a function because the stack offset is vital to be known beforehand. So a constant-sized pointer is passed. Obviously the classes are similar to this extent that the length of passed structures cannot be known in advance due to iheritance. However, the compiler somehow tricks you into the allocation of C++ classes. Then, you need to make sure that you get a copy constructor right - since else you would have a dissimitry between a "fake constructor" = bitwise copy, and a "real destructor", which is a source of either problems, or low efficiency. or both. You see, the C++ definition of a class being merely a structure is quite error-prone, and does not really correspond to the desired usage. And is harder to implement. A significant difference between D and Delphi is that D is more consistent in this matter, quite surprisingly! It takes the reference idea to the logical conclusion, and compares by default the contents of classes and not their adresses. I'm not sure there's any need in a pointer-to-struct variant - just another thing to be conserned of, since some programmers would choose to always handle their classes through pointer, others only through reference, and the third would simply loose themselves: "now, does this function expect a pointer, a reference, or a copy???"
 Frankly, your response was kind of unpleasant.

Confessing own mistakes hurts even more than not. :> -i.
Apr 09 2003