Hi everyone,

This is a code from Numerical Recipes book. It the vector class. However, I am a little confused about how this works.

[PHP]

template <class T>

class NRVec {

private:

int nn; //size of array. upper index is nn-1

T *v;

public:

NRVec();

explicit NRVec(int n); //Zero-based array

NRVec(const T &a, int n); //initialize to constant value

NRVec(const T *a, int n); //Initialize to array

NRVec(const NRVec &rhs); //Copy constructor

NRVec & operator=(const NRVec &rhs); //assignment

NRVec & operator=(const T &a); //assign a to every element

inline T & operator[](const int i); //i'th element

inline const T & operator[](const int i) const;

inline int size() const;

~NRVec();

};[/PHP]

My question is about the difference between two constructors:

[HTML]template <class T>

NRVec<T>::NRVec(const T&a, int n) : nn(n), v(new T[n])

{

for(int i=0; i<n; i++)

v[i]=a;

}

template <class T>

NRVec<T>::NRVec(const T *a, int n) :nn(n), v(new T[n])

{

for(int i=0; i<n; i++)

v[i]=*a++;

}[/HTML]

How do you think one would need both types of constructors? What is the main difference?

Hi,

For the first one

NRVec(const T&a, int n) : nn(n), v(new T[n])

it takes object by reference,this helps in handling the stack objects

NRVec(const T *a, int n) :nn(n), v(new T[n])

It takes pointer objects

This is what i got from it....

Others please add to it

Raghuram