看板 C_and_CPP 關於我們 聯絡資訊
#include <iostream> using namespace std; class Base { public: Base(int i) : m_j(i), m_i(m_j) {} Base() : m_j(0), m_i(m_j) {} int get_i() const { return m_i; } int get_j() const { return m_j; } private: int m_i; int m_j; }; int main() { Base obj(98); cout << obj.get_i() << endl << obj.get_j() << endl; return 0; } 程式初學者,想請問 1.int m_i; int m_j; 這個2個變數定義在下面,可是上面函式卻先使用了,為什麼不會發生錯誤呢? 2.Base() : m_j(0), m_i(m_j) {} 執行這一行的時候不會讓m_j變為0嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 123.110.76.190 ※ 文章網址: https://www.ptt.cc/bbs/C_and_CPP/M.1526990888.A.46C.html
bluesoul: 你呼叫的是Base(int) 05/22 20:31
skyrimrvks: 初始化列表的順序要跟data member宣告順序一致 05/23 11:57