C++,Need help to understand some constructors and functions in a vector class using pointers
Greetings All;
I have to develop a C++ class library comprising a collection of numerical techniques for scientific computing. The library should implement Vector class (using pointers) with some basic functionality stated in a header file "Vector.h".
#ifndef VECTOR_H
#define VECTOR_H
template <class T>
class CVector {
private:
int nn; //size of array
T *v; //pointer to array of data
public:
//Default constractor
CVector();
//zero based array
CVector(int n);
//initialize to constant of value a
CVector(int n, const T &a);
//initialize to array a
CVector(int n, const T *a);
//copy constractor
CVector(const CVector &rhs);
//assignment
CVector开发者_JAVA技巧 & operator=(const CVector &rhs);
//i'th element
inline T & operator[](const int i);
inline const T & operator[](const int i) const;
inline int size() const;
//resize (contents not preserved)
void resize(int newn);
//resize and assign a constant value
void assign(int newn, const T &a);
//deconstractor
~CVector();
};
#endif /* VECTOR_H */
I am a beginner in C++ and I have some confusion to understand some constructors and functions in the above code.
My questions are:
1- What is the concept of the following constructor?
//initialize to array a
CVector(int n, const T *a);
I mean how to initialize a vector to an array a?
2- What is the difference between the copy constructor and the assignment one?
//copy constractor
CVector(const CVector &rhs);
//assignment
CVector & operator=(const CVector &rhs);
3- I know that this function is to return the ith element of the vector:
//i'th element
inline T & operator[](const int i);
but what is the difference between it and this one:
inline const T & operator[](const int i) const;
I need to understand the concept in order to know how to implement them in the .cpp file and how to call them in my main. I'll be glad if you help me.
Best regards;
Q1: This constructor can be used to fill the vector with the contents of the n elements of the array starting at a.
For example:
float a[42] = { 31, 41, 59 };
CVector<float> v( 3, a );
Q2: The first is a copy constructor, the second is an assignment operator. The copy constructor is used to copy values into a function parameter, to return a value from a function, or to initialize a variable.
For example, the copy constructor is used for these:
CVector<float> foo( CVector<float> v ) { ... }
...
CVector<float> v1;
CVector<float> v2 = foo( v1 ); // Copy constructor used to pass in v1, and to return v2
CVector<float> v3 = v1; // Copy constructor used to copy v1 to v2.
And the assignment is used for this:
CVector<float> v4;
v4 = v1;
Q3. The first can be used on the left-hand-side of an assignment. The const version is used when applied to a const object.
void bar( const float & fval ) { ... }
...
CVector<float> v1( 3, a );
v1[0] = 42; // Non-const operator[]
const CVector<float> v2 = v1;
float value = v2[0]; // Const operator[]
bar( v2[0] ); // Const operator[]
1) set your members to: v = new T[n]; nn = n;
and copy the elements: for (int i = 0; i != n; ++i) v[i] = a[i];
2) Copy-assignment is when you already have an object, and want to assign a different value to it. Copy-constructor is when you want to create a new object with values from an existing one.
3) In c++ there is a concept of const functions: if you call the function on a const object; say: CVector<int> const& x = ...; x[3] = 3;
won't work, since x
is const. But for this to not work, the operator[]
needs to return a const &
to your internals.
FYI: if x
is non-const, the type of x[3]
is T &
because the non-const version of operator []
is used. If x
is const however, the type of x[3]
is const T &
because const version of operator []
is used.
Ok, I'm not a C++ guru, so hopefully someone a little more competent will chime in...but here is my $.02 on the subject.
Iterate over the array, adding each element to the vector. I am assuming that n is the number of elements in the array?
The difference between the copy constructor and the assignment one is probably semantic. They will have the same effect on the internal structure of the vector, but can be used in different situations.
This is just a guess, but I am thinking mutability is the difference. The second function returns an immutable T, meaning that it cannot be changed. The first function returns a mutable (changeable) T.
精彩评论