大家好;
我必须开发一个 C++ 类库,其中包含一系列用于科学计算的数值技术。该库应实现 Vector 类(使用指针),其中包含头文件“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 & 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 */
我是 C++ 的初学者,我对理解上述代码中的一些构造函数和函数有些困惑。
我的问题是:
1-以下构造函数的概念是什么?
//initialize to array a
CVector(int n, const T *a);
我的意思是如何将向量初始化为数组a?
2-复制构造函数和赋值之间有什么区别?
//copy constractor
CVector(const CVector &rhs);
//assignment
CVector & operator=(const CVector &rhs);
3-我知道这个函数是返回向量的第 i 个元素:
//i'th element
inline T & operator[](const int i);
但它和这个有什么区别:
inline const T & operator[](const int i) const;
我需要理解这个概念才能知道如何在 .cpp 文件中实现它们以及如何在我的 main 中调用它们。如果你能帮助我,我会很高兴的。
此致;