Posted on 2007-08-30 10:33
寶杉 閱讀(197)
評論(0) 編輯 收藏 引用 所屬分類:
C++
如果不想讓別人使用編譯器編寫構造拷貝和賦值函數(shù),可以聲明為私有:
class A
{ …
private:
A(const A &a); // 私有的拷貝構造函數(shù)
A & operate =(const A &a); // 私有的賦值函數(shù)
};
如果有人試圖編寫如下程序:
A b(a); // 調用了私有的拷貝構造函數(shù)
b = a; // 調用了私有的賦值函數(shù)
編譯器將指出錯誤,因為外界不可以操作A的私有函數(shù)。
但是怎樣才能使用構造拷貝和賦值函數(shù)呢?
虛擬函數(shù)使用:C++exams\destructor
在編寫派生類的賦值函數(shù)時,注意不要忘記對基類的數(shù)據(jù)成員重新賦值。例如:
C++exams\base_operator