Classes or stucts :: Newbie
Denis Koroskin
2korden at gmail.com
Sun Dec 19 12:26:24 PST 2010
On Mon, 20 Dec 2010 18:00:31 +0300, David Currie <curriedr at iinet.net.au>
wrote:
> I am new to D (like many have done C++ , Java ).
>
> Can a class be instantiated on the stack ?
>
> eg
>
> class C
> {
> private int _I1;
> private int _I2;
>
> public:
>
> this(int pI) // constructor
> {
> _I1 = pI;
> _I2 = pI + 1;
> }
>
> // ... other methods etc
> }
>
> void f() // just a function
> {
>
> C myC(3); // C++ syntax BUT is there a d equivalent
>
> }
>
> It appears that D ASSUMES myC is really a myC*(in C++)
>
> and therefore requires
>
> C myC = new C(3);
> // but this ALWAYS requires calling the memory allocator
> // this is what Java does (forces your Class instance onto the Heap)
>
> Is there any way in D to instantiate a stack object ?
>
> Will a struct do?
>
> Does a struct have a constructor (as opposed to an opcall?)
>
> I would be very grateful for a response.
>
> David Currie
>
>
Try the following:
scope c = new C(3);
Unfortunately last I've heard it's going to be deprecated in favor of a
library solution:
InSitu!(C) c = InSitu!(C)(3); // IIRC not implemented yet
More information about the Digitalmars-d-learn
mailing list