digitalmars.D.learn - What is difference between struct and class?
- Rnd (3/3) Jun 02 2019 I see that struct can have data as well as member functions and
- Adam D. Ruppe (5/6) Jun 02 2019 Classes support built-in runtime polymorphism through
- Rnd (4/10) Jun 02 2019 I am not clear if structs can have constructors (this) and
- Jonathan M Davis (17/29) Jun 02 2019 Yes structs can have constructors (but no default constructor - the defa...
- Rnd (4/13) Jun 03 2019 I know 'new' is not needed to create instances of structs but can
- Mike Parker (9/12) Jun 03 2019 Yes. It can be used with any value type to allocate a block of
- Mike Parker (4/9) Jun 03 2019 Ali's book has an example using a struct-based linked list in the
- Mike Parker (3/13) Jun 03 2019 Wrong link. It's at:
- Jonathan M Davis (11/28) Jun 03 2019 Yes, you can use new with structs, just like you can use it with ints or
- Rnd (6/12) Jun 03 2019 Also struct in D seem to be very similar to classes in C except
- Ron Tarrant (2/5) Jun 03 2019 Perhaps this will help: https://dlang.org/articles/ctod.html
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
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
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: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?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
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: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 DavisOn Monday, 3 June 2019 at 00:17:08 UTC, Rnd wrote: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?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
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:I know 'new' is not needed to create instances of structs but can one use 'new'? If yes, when should one use 'new'?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
Jun 03 2019
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
On Monday, 3 June 2019 at 08:47:41 UTC, Mike Parker wrote: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.orgIf 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
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:Wrong link. It's at: http://ddili.org/ders/d.en/pointers.htmlAli's book has an example using a struct-based linked list in the chapter on pointers: https://forum.dlang.org/thread/rkmcvxftykhsvxofpdtk forum.dlang.orgIf 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
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: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 DavisOn Sunday, June 2, 2019 9:40:43 PM MDT Rnd via Digitalmars-d-learn wrote:I know 'new' is not needed to create instances of structs but can one use 'new'? If yes, when should one use 'new'?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
Jun 03 2019
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 DavisAlso 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
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