[Groonga-mysql-commit] mroonga/mroonga [master] doc coding-style: add about use intialize list

Back to archive index

null+****@clear***** null+****@clear*****
2012年 3月 16日 (金) 10:02:21 JST


Kouhei Sutou	2012-03-16 10:02:21 +0900 (Fri, 16 Mar 2012)

  New Revision: a9f322b78bd83ab895025c38d5208f467bc95671

  Log:
    doc coding-style: add about use intialize list

  Modified files:
    doc/source/developer/coding_style.rst

  Modified: doc/source/developer/coding_style.rst (+35 -0)
===================================================================
--- doc/source/developer/coding_style.rst    2012-03-15 18:17:21 +0900 (3cd0d2a)
+++ doc/source/developer/coding_style.rst    2012-03-16 10:02:21 +0900 (583e0ac)
@@ -485,3 +485,38 @@ C++では真偽値に ``bool`` を使うためこのような状況は発生し
      if (!memcmp(value1, value2, value_size)) {
        printf("same value!\n");
      }
+
+初期化
+------
+
+メンバー変数の初期化には初期化リストを用いる
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+無駄な処理を省くためにコンストラクターでのメンバー変数の初期化には初期化リストを用いる。初期化リストを用いないとコンストラクターの処理とコピーコンストラクター・代入処理が行われたりなど非効率である。(後述)
+
+よい例:
+
+    class Table
+    {
+      Table(const char *name);
+      std::string name_;
+    }
+
+    Table::Table(const char *name) :
+      name_(name)
+    {
+    }
+
+悪い例( ``std::string(name)`` のところでコンストラクターが動き、 ``name_ = ...`` のところで代入演算子が動いて2回初期化している):
+
+    class Table
+    {
+      Table(const char *name);
+      std::string name_;
+    }
+
+    Table::Table(const char *name)
+    {
+      name_ = std::string(name);
+    }
+




Groonga-mysql-commit メーリングリストの案内
Back to archive index