By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,079 Members | 901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,079 IT Pros & Developers. It's quick & easy.

vector class from pointers

P: 71
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?
Jul 19 '07 #1
Share this Question
Share on Google+
2 Replies


gpraghuram
Expert 100+
P: 1,275
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
Jul 20 '07 #2

weaknessforcats
Expert Mod 5K+
P: 9,197
This constructor:
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;
}
is the copy constructor. This is called by the compiler to make a copy of the vector. That is, the new vector is a copy of the argument vector. This constructor will be implicitly called shoudl you use an NRVec as a function argument.

This constructor:
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++;
}
is a constructor used to intialize a vector using an int array as an argument.
Jul 22 '07 #3

Post your reply

Sign in to post your reply or Sign up for a free account.