ref?

Rainer Deyke rainerd at eldwood.com
Sun Feb 15 21:18:48 PST 2009


Jerry Quinn wrote:
> If you want inheritance, copy semantics are an issue.  For example,
> if you have struct A : B, and an array of B, you can't put an A in
> it, since there's not enough space for an A (unless A adds 0
> storage).  If you have an array of A, inheritance isn't really buying
> you anything over having a B as the first member of A.  Any place
> where you'd want to use A as a B, you can get to the member B struct
> directly.

There are really three separate issues here: copy policy, storage
policy, and clean-up policy.

Copy policy: what are the semantics of 'a = b;'?
  Reference semantics:
    'a' becomes a reference to the same object as 'b'.
  Value semantics:
    The object 'b' is copied into 'a'.

Storage policy: given 'A a;', where is the object stored?
  Direct storage:
     Directly in the variable 'a'.
  Padded direct storage:
     Directly in the variable 'a', but with enough padding to also store
     an object of any subtype of 'A'.  Requires that the full set of the
     subtypes of 'A' be known at compile time.
  Heap storage:
    The object is stored on the heap; the variable 'a' merely contains a
    pointer.

Clean-up policy: given 'A a;', when is 'a''s destructor called?
  RAII:
    Immediately when 'a' goes out of scope.  The object still exists at
    this point.
  Garbage collection:
    Sometime after the last reference to the object is no longer
    reachable from any global or stack variable.  By the time the
    destructor is called, other objects referenced by the object may
    already have been destroyed.


These three policies are mostly conceptually orthogonal, although
garbage collection requires heap storage.  Interesting combinations include:
  Reference semantics, heap storage, garbage collection:
    Classes in D.
  Value semantics, direct storage, RAII:
    Structs in D.
  Value semantics, padded direct storage, RAII:
    One possible approach to value types with inheritance but without
    slicing.
  Value semantics, heap storage, RAII:
    Another possible approach to value types with inheritance but
    without slicing.
  Reference semantics, heap storage, RAII (with reference counting):
    Reference types with destructors that actually work.


-- 
Rainer Deyke - rainerd at eldwood.com



More information about the Digitalmars-d mailing list