我是相當新的面向對象的C++,我試圖做一個構造這樣一個鏈表:我可以用大括號構造一個鏈表嗎?
某處在List.h我們就會有這樣的:
struct Node
{
int data;
Node *next;
};
然後在main.cpp中,我希望能夠建立這樣的名單:
int main()
{
List A({1,2,3,4,5,6});// I want the amount of numbers to indicate the size of
//the list and the numbers to go into each node in order
return 0;
}
所以我的問題是,我可以做出這樣的構造?如果是的話那麼如何?我必須使用模板嗎?我試圖找到這樣的問題在SO中回答,但他們都包括模板,我還沒有學到。如果我可以讓我的構造函數執行此操作,是否可以在不使用模板的情況下執行此操作?
我認爲這在C++ 11中是可行的。 – paddy
看看http://www.cplusplus.com/reference/initializer_list/initializer_list/ – payo
@payo,那將是一個*模板*。 – WhozCraig