2011-04-29 40 views
0

問候所有;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文件中實現它們,以及如何在我的主文件中調用它們。如果你幫我,我會很高興。

此致敬意;

+0

圖書館評論似乎有一些錯別字。當它說'初始化數組'時,它可能意味着'從數組a中初始化**'。另外,我不認爲它被稱爲「構造器」,而是「構造器」。 – 2011-04-29 14:25:18

回答

1

Q1:這個構造函數可以用來填充矢量,其中數組的n個元素的內容從a開始。

例如:

float a[42] = { 31, 41, 59 }; 
    CVector<float> v(3, a); 

Q2:第一種是拷貝構造,第二個是一個賦值運算符。複製構造函數用於將值複製到函數參數中,從函數返回值或初始化變量。

例如,拷貝構造函數用於這些:

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. 

而且分配用於此:

CVector<float> v4; 
v4 = v1; 

Q3。第一個可以在作業的左側使用。應用於const對象時使用const版本。

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

對你來說Q3例子中的非const操作符[]就足夠了。 – 2011-04-29 14:29:11

+1

這兩個都使用非const版本:'float value = v1 [0]; bar(v1 [0]);' – 2011-04-29 14:39:28

+0

哎呀,你們都是對的。固定在上面。 – 2011-04-29 16:09:27

0

好吧,我不是一個C++大師,所以希望別人多了幾分主管將幫腔......但這裏是我關於這個問題0.02 $。

  1. 遍歷數組,將每個元素添加到向量中。我假設n是數組中元素的數量?

  2. 複製構造函數和賦值之間的區別可能是語義的。它們對載體的內部結構具有相同的效果,但可用於不同的情況。

  3. 這只是一個猜測,但我想可變性是不同的。第二個函數返回一個不可變的T,這意味着它不能被改變。第一個函數返回一個可變(可變)T.

1

1)設置您的會員:v = new T[n]; nn = n; 和複製的元素:for (int i = 0; i != n; ++i) v[i] = a[i];

2)拷貝賦值是當你已經有一個對象,並想爲其分配不同的值。複製構造函數是當你想創建一個新的對象與現有的值。 3)在C++中有一個const函數的概念:如果你在一個const對象上調用函數,說:CVector<int> const& x = ...; x[3] = 3;將無法​​正常工作,因爲x是常量。但是爲了不起作用,operator[]需要返回const &到你的內部。
僅供參考:如果x非常量,則x[3]的類型爲T &,因爲使用了非常量版本的operator []。但是,如果x爲常量,則x[3]的類型爲const T &,因爲使用了const版本operator []

+0

謝謝你的幫助,但我仍然沒有得到Q3的答案。 – Daisy 2011-05-07 18:20:00

+0

@Daisy在Q3上寫了一些更多,但我想現在已經太遲了:) – Jan 2011-07-24 18:10:25