Click to See Complete Forum and Search --> : help in dynamic array declaration


abul
July 28th, 2005, 12:01 AM
template <class AType,int size> class atype
{
// AType a[size];
AType *a=new AType[size];



When i am declaring an array in the above way an error is being shown.
plz help me to solve it.

the error is as follows

cant initialize a class memeber here.
I m new in dis areana.

Kheun
July 28th, 2005, 12:19 AM
You have to allocate memory in the constructor and then releasing in the destructor.


template <class AType,int size> class atype
{
AType *a;

public:
atype()
{
a = new AType[size]; // Allocating memory.
}

virtual ~atype()
{
delete []a; // Don't forget to release memory when not needed.
}
};

abul
July 28th, 2005, 12:41 AM
thank u.its working fine.