Classes or stucts :: Newbie

David Currie curriedr at iinet.net.au
Mon Dec 20 07:00:31 PST 2010


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




More information about the Digitalmars-d-learn mailing list