Skip to content

Instantly share code, notes, and snippets.

@kuanyui
Last active March 7, 2018 17:46
Show Gist options
  • Save kuanyui/57e4ac6e5ae972fe30612114e43a7933 to your computer and use it in GitHub Desktop.
Save kuanyui/57e4ac6e5ae972fe30612114e43a7933 to your computer and use it in GitHub Desktop.

放在function中的話:

MyObj a;            // 1. 宣告a並立刻呼叫他的default ctor(initialization)
MyObj b {123}       // 2. 宣告a並立刻呼叫他對應type的ctor(initialization)
MyObj *b;           // 3. 宣告一個可以指向MyObj 的instance的指標

如果拿MyObj當作另一個class的member variable:

class Foo {
public:
  Foo () { /* ctor body ...*/ }                            // 7. default ctor,此ctor的body執行前就會呼叫 4, 5
  Foo (int i) : m_a{i}, m_b{i}  { /* ctor body ...*/ }     // 8. 頭好痛
private:
  MyObj m_a;           // 4. 這個到底會怎樣?Foo::Foo()的body執行前就摳他? 像是 8.就有指定他的值,這樣會有什麼行為?優先按照Foo::Foo(int) 的initialize line裡面的初始值來摳?還是照摳MyObj的default ctor?
  MyObj m_b {123}      // 5. 同上,這個到底會怎樣? 
  MyObj *m_c;          // 6. 這樣會宣告一個可以指向MyObj的指標(沒啥其他問題)
}

m_am_b 如果在 /* ctor body */ 中使用到,兩者應該都已經在initialize line中被初始化過?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment