問候所有;C++,需要幫助來理解矢量類中使用指針的一些構造函數和函數
我必須開發一個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文件中實現它們,以及如何在我的主文件中調用它們。如果你幫我,我會很高興。
此致敬意;
圖書館評論似乎有一些錯別字。當它說'初始化數組'時,它可能意味着'從數組a中初始化**'。另外,我不認爲它被稱爲「構造器」,而是「構造器」。 – 2011-04-29 14:25:18