fbpx
维基百科

三法則

三法則(英語:Rule of Three)以及五法則C++裡,它是一個以設計的基本原則而制定的定律。

規則

它的要求是,假如有明顯定義英语Declaration (computer programming)下列其中一個成員函式,那麼程序员必須寫入其他兩個成員函式到類別內,也就是說下列三個成員函式缺一不可[註 1] [1]

上述三個函式是特別的成員函式英语Special member functions,假如程式設計師沒有自行定義或宣告這三個函式,編譯器會自動地建立他們並且編譯到應用程式內。然而,如果程式設計師僅定義其中一個,其餘兩個函式仍然會由編譯器自動產生,這種混雜的情況非常容易產生程式設計師難以預期的錯誤。三法則的存在,正是提醒程式設計師避免那樣的陷阱。 三法則這個專有名詞是由馬歇爾·克來恩於1991年創立[註 2][2]

它的修正版本是,假如類別有用到RAII,可以不必定義解構子,也就是所謂的二大定律[註 3][3]。 因為隱性產生的建構子與設定運算子可以很容易地複製類別內所有的資料成員[4],當資料成員是指標型態時,指標位址會隨著類別而跟著被複製[註 4]。要注意的是,直接地複製指標位址是一項非常危險的動作,所以只要類別有封裝指標型態的資料結構,或是類別有封裝外部參照的資料成員,例如指標型態的資料成員,程式設計師應該為此而定義顯性的複製建構子與設定運算子[註 5]

五法則

C++11新增兩個法則,稱為五法则[註 6]

  • 解構子
  • 複製建構子
  • 設定運算子
  • 移動建構子[註 7]
  • 移動复制运算符[註 8]

範例

C/C++ 原始碼《計算方塊體積》三法則範例
头文件 header.h 主函式 main.cpp
#ifndef _HEADER_H_ #define _HEADER_H_ // // 判斷是否為微軟編譯器 #ifndef _MSC_VER #undef NULL #define NULL 0 #endif // #include <iostream> #include <limits> // using std::cin; using std::cout; using std::endl; // // 類別:方塊 class CCube { public:  // 建構子  CCube();  // 含有參數的建構子  CCube(double length, double width, double height);  // 三法則:解構子  ~CCube();  // 三法則:複製建構子  CCube(const CCube &sample);  // 三法則:設定運算子  CCube& operator=(const CCube &sample);  // 設定長寬高  void setLength(double length);  void setWidth(double width);  void setHeight(double height);  // 取得長寬高  double getLength() const;  double getWidth() const;  double getHeight() const;  // 計算體積  double getVolume() const; protected: private:  // 長寬高  double m_Length;  double m_Width;  double m_Height; }; // void PAUSE(void); // #endif 
#include"header.h" // // 判斷是否為微軟編譯器 #ifndef _MSC_VER int #else void #endif main(int argc, char* argv[]) {  // 方塊零  CCube cube0(4.3, 5.2, 6.1);  // 第一個方塊  {  cout << "=== No.1 cube ===" << endl;  CCube cube1 = cube0;  cout << "Volume of cube = " << cube1.getVolume() << endl;  }  // 第二個方塊  {  cout << "=== No.2 cube ===" << endl;  CCube cube2;  cube2 = cube0;  cout << "Volume of cube = " << cube2.getVolume() << endl;  }  PAUSE();  return #ifndef _MSC_VER  EXIT_SUCCESS #endif  ; } 
头文件實作 header.cpp
#include "header.h" // void PAUSE(void) {  cin.clear();  cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');  cout << "press any key to continue...";  cin.get(); } CCube::CCube() {  cout << "Constructor: CCube()" << endl;  this->m_Length = 0.0;  this->m_Width = 0.0;  this->m_Height = 0.0; } CCube::CCube(double length, double width, double height) {  cout << "Constructor: CCube(length, width, height)" << endl;  this->m_Length = length;  this->m_Width = width;  this->m_Height = height; } CCube::~CCube() {  cout << "Destructor: ~CCube()" << endl;  this->m_Length = 0.0;  this->m_Width = 0.0;  this->m_Height = 0.0; } CCube::CCube(const CCube &sample) {  cout << "Copy constructor: CCube(const CCube &sample)" << endl;  //  // 保護:禁止設值給自己  if (this != &sample)  {  this->m_Length = sample.m_Length;  this->m_Width = sample.m_Width;  this->m_Height = sample.m_Height;  } } CCube& CCube::operator=(const CCube &sample) {  cout << "Assignment operator: operator=(const CCube &sample)" << endl;  //  // 保護:禁止設值給自己  if (this != &sample)  {  this->m_Length = sample.m_Length;  this->m_Width = sample.m_Width;  this->m_Height = sample.m_Height;  }  return *this; } double CCube::getVolume() const {  return (this->m_Length * this->m_Width * this->m_Height); } 

注釋

  1. ^ 三法則,英語:Rule of Three;三大定律,英語:the Law of The Big Three;大三律,英語:The Big Three
  2. ^ 馬歇爾·克來恩,英語:Marshall Cline
  3. ^ 二大定律,英語:The Law of The Big Two
  4. ^ 隱性產生,英語:implicitly-generated,由編譯器自動產生
  5. ^ 顯性,英語:explicit,由程式設計師來編寫清楚明確的定義
  6. ^ 五法则,英語:Rule of Five
  7. ^ 移動建構子,英語:move constructor
  8. ^ 移動指定運算子,英語:move assignment operator

參考資料

  1. ^ Stroustrup, Bjarne. The C++ Programming Language 3. Addison-Wesley. 2000: 283–4. ISBN 978-0201700732. 
  2. ^ Koenig, Andrew; Barbara E. Moo. C++ Made Easier: The Rule of Three. Dr. Dobb's Journal. 2001-06-01 [2009-09-08]. 
  3. ^ Karlsson, Bjorn; Wilson, Matthew. The Law of the Big Two. The C++ Source. Artima. 2004-10-01 [2008-01-22]. (原始内容存档于2012-03-17). 
  4. ^ 比雅尼·史特勞斯特魯普. The C++ Programming Language. : 第 271 頁. 

相關條目

三法則, 此條目可参照英語維基百科相應條目来扩充, 2022年11月15日, 若您熟悉来源语言和主题, 请协助参考外语维基百科扩充条目, 请勿直接提交机械翻译, 也不要翻译不可靠, 低品质内容, 依版权协议, 译文需在编辑摘要注明来源, 或于讨论页顶部标记, href, template, translated, page, html, title, template, translated, page, translated, page, 标签, 英語, rule, three, 以及五法則在c, 它是一個以設計. 此條目可参照英語維基百科相應條目来扩充 2022年11月15日 若您熟悉来源语言和主题 请协助参考外语维基百科扩充条目 请勿直接提交机械翻译 也不要翻译不可靠 低品质内容 依版权协议 译文需在编辑摘要注明来源 或于讨论页顶部标记 a href Template Translated page html title Template Translated page Translated page a 标签 三法則 英語 Rule of Three 以及五法則在C 裡 它是一個以設計的基本原則而制定的定律 目录 1 規則 2 五法則 3 範例 4 注釋 5 參考資料 6 相關條目規則 编辑它的要求是 假如类有明顯定義 英语 Declaration computer programming 下列其中一個成員函式 那麼程序员必須寫入其他兩個成員函式到類別內 也就是說下列三個成員函式缺一不可 註 1 1 解構子 複製建構子 設定運算子 C 上述三個函式是特別的成員函式 英语 Special member functions 假如程式設計師沒有自行定義或宣告這三個函式 編譯器會自動地建立他們並且編譯到應用程式內 然而 如果程式設計師僅定義其中一個 其餘兩個函式仍然會由編譯器自動產生 這種混雜的情況非常容易產生程式設計師難以預期的錯誤 三法則的存在 正是提醒程式設計師避免那樣的陷阱 三法則這個專有名詞是由馬歇爾 克來恩於1991年創立 註 2 2 它的修正版本是 假如類別有用到RAII 可以不必定義解構子 也就是所謂的二大定律 註 3 3 因為隱性產生的建構子與設定運算子可以很容易地複製類別內所有的資料成員 4 當資料成員是指標型態時 指標位址會隨著類別而跟著被複製 註 4 要注意的是 直接地複製指標位址是一項非常危險的動作 所以只要類別有封裝指標型態的資料結構 或是類別有封裝外部參照的資料成員 例如指標型態的資料成員 程式設計師應該為此而定義顯性的複製建構子與設定運算子 註 5 五法則 编辑C 11新增兩個法則 稱為五法则 註 6 解構子 複製建構子 設定運算子 移動建構子 註 7 移動复制运算符 註 8 範例 编辑C C 原始碼 計算方塊體積 三法則範例 头文件 header h 主函式 main cpp ifndef HEADER H define HEADER H 判斷是否為微軟編譯器 ifndef MSC VER undef NULL define NULL 0 endif include lt iostream gt include lt limits gt using std cin using std cout using std endl 類別 方塊 class CCube public 建構子 CCube 含有參數的建構子 CCube double length double width double height 三法則 解構子 CCube 三法則 複製建構子 CCube const CCube amp sample 三法則 設定運算子 CCube amp operator const CCube amp sample 設定長寬高 void setLength double length void setWidth double width void setHeight double height 取得長寬高 double getLength const double getWidth const double getHeight const 計算體積 double getVolume const protected private 長寬高 double m Length double m Width double m Height void PAUSE void endif include header h 判斷是否為微軟編譯器 ifndef MSC VER int else void endif main int argc char argv 方塊零 CCube cube0 4 3 5 2 6 1 第一個方塊 cout lt lt No 1 cube lt lt endl CCube cube1 cube0 cout lt lt Volume of cube lt lt cube1 getVolume lt lt endl 第二個方塊 cout lt lt No 2 cube lt lt endl CCube cube2 cube2 cube0 cout lt lt Volume of cube lt lt cube2 getVolume lt lt endl PAUSE return ifndef MSC VER EXIT SUCCESS endif 头文件實作 header cpp include header h void PAUSE void cin clear cin ignore std numeric limits lt std streamsize gt max n cout lt lt press any key to continue cin get CCube CCube cout lt lt Constructor CCube lt lt endl this gt m Length 0 0 this gt m Width 0 0 this gt m Height 0 0 CCube CCube double length double width double height cout lt lt Constructor CCube length width height lt lt endl this gt m Length length this gt m Width width this gt m Height height CCube CCube cout lt lt Destructor CCube lt lt endl this gt m Length 0 0 this gt m Width 0 0 this gt m Height 0 0 CCube CCube const CCube amp sample cout lt lt Copy constructor CCube const CCube amp sample lt lt endl 保護 禁止設值給自己 if this amp sample this gt m Length sample m Length this gt m Width sample m Width this gt m Height sample m Height CCube amp CCube operator const CCube amp sample cout lt lt Assignment operator operator const CCube amp sample lt lt endl 保護 禁止設值給自己 if this amp sample this gt m Length sample m Length this gt m Width sample m Width this gt m Height sample m Height return this double CCube getVolume const return this gt m Length this gt m Width this gt m Height 注釋 编辑 三法則 英語 Rule of Three 三大定律 英語 the Law of The Big Three 大三律 英語 The Big Three 馬歇爾 克來恩 英語 Marshall Cline 二大定律 英語 The Law of The Big Two 隱性產生 英語 implicitly generated 由編譯器自動產生 顯性 英語 explicit 由程式設計師來編寫清楚明確的定義 五法则 英語 Rule of Five 移動建構子 英語 move constructor 移動指定運算子 英語 move assignment operator參考資料 编辑 Stroustrup Bjarne The C Programming Language 3 Addison Wesley 2000 283 4 ISBN 978 0201700732 Koenig Andrew Barbara E Moo C Made Easier The Rule of Three Dr Dobb s Journal 2001 06 01 2009 09 08 引文使用过时参数coauthors 帮助 Karlsson Bjorn Wilson Matthew The Law of the Big Two The C Source Artima 2004 10 01 2008 01 22 原始内容存档于2012 03 17 引文使用过时参数coauthors 帮助 比雅尼 史特勞斯特魯普 The C Programming Language 第 271 頁 相關條目 编辑C 類 类 计算机科学 取自 https zh wikipedia org w index php title 三法則 amp oldid 74630212, 维基百科,wiki,书籍,书籍,图书馆,

文章

,阅读,下载,免费,免费下载,mp3,视频,mp4,3gp, jpg,jpeg,gif,png,图片,音乐,歌曲,电影,书籍,游戏,游戏。