I am just a novice in C++ and I am trying to make some of my first classes for my work. However, when thinking about the structure of the class, I am doubting and a little bit confused between a template class and inner parameter instancing using free store memory.
Let's consider a simple example as follows.
1 - Template class
template < unsigned int n, typename T>
class myclass
{
private:
T _data[n];
public:
myclass()
{
for (int i=0;i<n;i++) _data[i] = 0;
}
~myclass(){}
}
2 - Heap memory use
template < typename T>
class myclass
{
private:
unsigned int _n;
T *_data;
public:
myclass(const unsigned int &m)
{
_n = m;
_data = new T[_n];
for (int i=0;i<n;i++) _data[i] =0.;
}
~myclass(){ delete [] _data;}
}
Between the two aforementioned methods, which one is better? What is the pros and cons of those two? Would anyone please helping me answering those questions? Thank you very much.