What is difference between struct and class?

Jonathan M Davis newsgroup.d at jmdavisprog.com
Mon Jun 3 08:54:12 UTC 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:
> > 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





More information about the Digitalmars-d-learn mailing list