Struct vs Class

Hasan Aljudy hasan.aljudy at gmail.com
Mon Dec 11 12:32:33 PST 2006


Well, these are kinda the superficial differences.

The main one being classes are used for OOP, so, they support 
polymorphism (runtime binding of function calls), and objects have an 
identity. i.e. two objects are different objects, even if they have the 
same state at this moment in time.

Where as structs are meant to be simple aggregates of data. Instances of 
a struct have no identity.

Derek Parnell wrote:
> Have I got the difference between Class and Struct right? Have I missed
> anything?
> 
> Functionality              Class          Struct
> --------------------------------------------------
> Default instantiation  ::  Heap           Stack
> Constructor            ::  this()         void opCall()
> Destructor             ::  ~this()        None
> Argument passing       ::  by Reference   by Value
> Assignment             ::  Reference      Value
>                              bit copy        bit copy
> Inheritance            ::  Single         None
> Interfaces             ::  Multiple       None
> Order of data members  ::  Defined by     Defined by
>   in RAM                     compiler       coder
> -------------------------------------------------
> 
> 


More information about the Digitalmars-d-learn mailing list