www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - What is difference between struct and class?

reply Rnd <r_narang yahoo.com> writes:
I see that struct can have data as well as member functions and 
instances can be created. So they sound like classes only.

What additional features do classes offer in D?
Jun 02 2019
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
On Monday, 3 June 2019 at 00:17:08 UTC, Rnd wrote:
 What additional features do classes offer in D?
Classes support built-in runtime polymorphism through inheritance. structs don't. As a result of this, classes are a little bit heavier resource-wise and are semantically always object references.
Jun 02 2019
parent reply Rnd <r_narang yahoo.com> writes:
On Monday, 3 June 2019 at 00:47:27 UTC, Adam D. Ruppe wrote:
 On Monday, 3 June 2019 at 00:17:08 UTC, Rnd wrote:
 What additional features do classes offer in D?
Classes support built-in runtime polymorphism through inheritance. structs don't. As a result of this, classes are a little bit heavier resource-wise and are semantically always object references.
I am not clear if structs can have constructors (this) and whether they can be multiple? Also can data be made private and getters and setters used to access them?
Jun 02 2019
parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Sunday, June 2, 2019 9:40:43 PM MDT Rnd via Digitalmars-d-learn wrote:
 On Monday, 3 June 2019 at 00:47:27 UTC, Adam D. Ruppe wrote:
 On Monday, 3 June 2019 at 00:17:08 UTC, Rnd wrote:
 What additional features do classes offer in D?
Classes support built-in runtime polymorphism through inheritance. structs don't. As a result of this, classes are a little bit heavier resource-wise and are semantically always object references.
I am not clear if structs can have constructors (this) and whether they can be multiple? Also can data be made private and getters and setters used to access them?
Yes structs can have constructors (but no default constructor - the default value of a struct is its init value, which is defined by the values that the struct's members are directly initialized with), and structs can have all of the various functions that a class can have. The can also use private, public, and package just like classes can (but not protected, since structs have no inheritance). Basically, structs go wherever they're declared and don't have inheritance, whereas classes are always reference types and have inheritance. In general, besides that, their abilities are pretty much the same, though there are some differences that stem from the fact that classes are always reference types, whereas structs aren't. I'd advise reading http://ddili.org/ders/d.en/index.html If you want to know more about structs and classes specifically, then you can go straight to the sections on them, but you're going to understand a lot of things better if you just read through the book. - Jonathan M Davis
Jun 02 2019
parent reply Rnd <r_narang yahoo.com> writes:
On Monday, 3 June 2019 at 06:01:15 UTC, Jonathan M Davis wrote:
 On Sunday, June 2, 2019 9:40:43 PM MDT Rnd via 
 Digitalmars-d-learn wrote:
 On Monday,
http://ddili.org/ders/d.en/index.html If you want to know more about structs and classes specifically, then you can go straight to the sections on them, but you're going to understand a lot of things better if you just read through the book. - Jonathan M Davis
I know 'new' is not needed to create instances of structs but can one use 'new'? If yes, when should one use 'new'?
Jun 03 2019
next sibling parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 3 June 2019 at 07:13:44 UTC, Rnd wrote:

 I know 'new' is not needed to create instances of structs but 
 can one use 'new'?
Yes. It can be used with any value type to allocate a block of memory on the GC heap and return a pointer to that memory: struct Foo { ... } Foo* f = new Foo; int* i = new int;
 If yes, when should one use 'new'?
Whenever you need to allocate something from the GC heap. In my experience, it's rare to need it with value types in D. I tend to use it primarily with classes and arrays.
Jun 03 2019
parent reply Mike Parker <aldacron gmail.com> writes:
On Monday, 3 June 2019 at 08:47:41 UTC, Mike Parker wrote:

 If yes, when should one use 'new'?
Whenever you need to allocate something from the GC heap. In my experience, it's rare to need it with value types in D. I tend to use it primarily with classes and arrays.
Ali's book has an example using a struct-based linked list in the chapter on pointers: https://forum.dlang.org/thread/rkmcvxftykhsvxofpdtk forum.dlang.org
Jun 03 2019
parent Mike Parker <aldacron gmail.com> writes:
On Monday, 3 June 2019 at 08:50:46 UTC, Mike Parker wrote:
 On Monday, 3 June 2019 at 08:47:41 UTC, Mike Parker wrote:

 If yes, when should one use 'new'?
Whenever you need to allocate something from the GC heap. In my experience, it's rare to need it with value types in D. I tend to use it primarily with classes and arrays.
Ali's book has an example using a struct-based linked list in the chapter on pointers: https://forum.dlang.org/thread/rkmcvxftykhsvxofpdtk forum.dlang.org
Wrong link. It's at: http://ddili.org/ders/d.en/pointers.html
Jun 03 2019
prev sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
On Monday, June 3, 2019 1:13:44 AM MDT Rnd via Digitalmars-d-learn wrote:
 On Monday, 3 June 2019 at 06:01:15 UTC, Jonathan M Davis wrote:
 On Sunday, June 2, 2019 9:40:43 PM MDT Rnd via

 Digitalmars-d-learn wrote:
 On Monday,
http://ddili.org/ders/d.en/index.html If you want to know more about structs and classes specifically, then you can go straight to the sections on them, but you're going to understand a lot of things better if you just read through the book. - Jonathan M Davis
I know 'new' is not needed to create instances of structs but can one use 'new'? If yes, when should one use 'new'?
Yes, you can use new with structs, just like you can use it with ints or floats or almost any type. It puts the struct on the heap instead of the stack. When that makes sense depends on when you need to have a struct on the heap instead of the stack. It's basically the same as why you'd want to put a class without inheritance on the heap in C++. structs in D are basically the same as C++ classes that don't have inheritance and can be put on the stack or the heap, and classes in D are akin to C++ classes that use inheritance and are always put on the heap and used via pointers. D classes are similar to Java classes in that respect. - Jonathan M Davis
Jun 03 2019
parent reply Rnd <r_narang yahoo.com> writes:
On Monday, 3 June 2019 at 08:54:12 UTC, Jonathan M Davis wrote:
 structs in D are basically the same as C++ classes that don't 
 have inheritance and can be put on the stack or the heap, and 
 classes in D are akin to C++ classes that use inheritance and 
 are always put on the heap and used via pointers. D classes are 
 similar to Java classes in that respect.

 - Jonathan M Davis
Also struct in D seem to be very similar to classes in C except lack of inheritance. These similarities and differences should be highlighted in documentation etc since many new users have at least some knowledge of C/C++ and understanding will be easier.
Jun 03 2019
parent Ron Tarrant <rontarrant gmail.com> writes:
On Monday, 3 June 2019 at 09:43:25 UTC, Rnd wrote:

 These similarities and differences should be highlighted in 
 documentation etc since many new users have at least some 
 knowledge of C/C++ and understanding will be easier.
Perhaps this will help: https://dlang.org/articles/ctod.html
Jun 03 2019