www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Struct vs Class

reply Derek Parnell <derek nomail.afraid.org> writes:
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
-------------------------------------------------


-- 
Derek
(skype: derek.j.parnell)
Melbourne, Australia
"Down with mediocrity!"
11/12/2006 3:04:33 PM
Dec 10 2006
next sibling parent "Jarrett Billingsley" <kb3ctd2 yahoo.com> writes:
"Derek Parnell" <derek nomail.afraid.org> wrote in message 
news:1jh0lfbbfm2rr.1nza8l988sfh1.dlg 40tude.net...

 Constructor            ::  this()         void opCall()

Isn't it usually "static StructName opCall()"? Unless you use another convention.
Dec 10 2006
prev sibling next sibling parent Bill Baxter <dnewsgroup billbaxter.com> writes:
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
 -------------------------------------------------
 
 

Also you should include the new features for overloading opAssign and the implicit call of static opCall. (I think there's some difference there for structs and classes, though I haven't looked at it too closely yet). --bb
Dec 10 2006
prev sibling next sibling parent reply Karen Lanrap <karen digitaldaemon.com> writes:
Derek Parnell wrote:

 Have I missed anything?
 
 Functionality              Class          Struct
 --------------------------------------------------

Access to outer elements from nested constructs Yes No "outer" property for "this" Yes No
 -------------------------------------------------

Dec 10 2006
parent reply Stewart Gordon <smjg_1998 yahoo.com> writes:
Karen Lanrap wrote:
 Derek Parnell wrote:
 
 Have I missed anything?

 Functionality              Class          Struct
 --------------------------------------------------

Access to outer elements from nested constructs Yes No "outer" property for "this" Yes No
 -------------------------------------------------


These are really just one difference: the concept of outer objects. Stewart.
Dec 12 2006
parent Karen Lanrap <karen digitaldaemon.com> writes:
Stewart Gordon wrote:

 These are really just one difference: the concept of outer objects.

Correct. "including" combines them.
Dec 12 2006
prev sibling next sibling parent "Andrei Alexandrescu (See Website For Email)" <SeeWebsiteForEmail erdani.org> writes:
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
 -------------------------------------------------

Overridable (virtual) functions: yes/no. Unless that's self-understood under "inheritance". Lockable (can be the target of "synchronized"): yes/no. Andrei
Dec 10 2006
prev sibling next sibling parent Paolo Invernizzi <arathorn NOSPAM_fastwebnet.it> writes:
Derek Parnell wrote:
 Have I got the difference between Class and Struct right? Have I missed
 anything?

One big thing I'm missing... invariants! Really handy for catching bugs in struct like point/rectangle etc... --- Paolo Invernizzi
Dec 11 2006
prev sibling parent Hasan Aljudy <hasan.aljudy gmail.com> writes:
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
 -------------------------------------------------
 
 

Dec 11 2006