2015-11-15 170 views
0

我想在另一個類的構造函數中初始化一個std ::數組的對象。看來聚合初始化應該在這裏工作,但我無法弄清楚適當的語法。我如何去做這件事?在類的構造函數中初始化std ::類的數組

class A { 
     const int a; 
public: 
     A(int an_int) : a(an_int) {} 
}; 

class B { 
     std::array<A,3> stuff; 
public: 
     B() : 
     stuff({1,2,3}) // << How do I do this? 
     {} 
}; 

int main() { 
     B b; 
     return 0; 
} 

回答

4

你只需要一個額外的一對大括號:

B() : stuff({{1,2,3}}) {} 
      ^ ^

或者可以替換括號與括號:

B() : stuff {{1,2,3}} {} 
      ^ ^
相關問題