2015-05-23 47 views
-1

我需要能夠根據字符串所說的內容動態創建和檢索結構,並返回相應的float。我可以將它們放置在一組沒有問題,但它是動態創建,我只是做我使用不get.The結構是這樣的:C++:動態添加結構到容器並檢索它們

struct structure 
{ 
    std::string string1 = "default"; 
    int integer1 = 0; 
}; 

任何幫助表示讚賞:)

+0

您可以使用[的std ::向量(http://www.cplusplus.com/reference/vector/vector/)爲您的動態容器的需求,但Novelocrat有一個更好的主意,你有[STD :: map](http://www.cplusplus.com/reference/map/map/) – wizebin

+0

我想你正在尋找一個工廠方法模式(http://en.wikipedia.org/wiki/Factory_method_pattern)。 –

回答

0

我相信你需要一個工廠來創建對象嗎?

structure* createStructur(std::string name) 
{ 
    if (name == "foo") 
    { 
     return new ...; 
    } 
    else if (name == "bar") 
    { 
     return new ...; 
    } 
    ... 
} 
相關問題