我要說的是,也許C++不是你想要完成的任何事情的正確語言。你越想通過外部數據來控制你的程序,你就越接近編寫腳本的程度。當這是你的目標時,有很多更強大的選擇。
這就是說,當然有可能,雖然不是那麼容易。想到兩種方法。首先是要求所有適用的類型都有一個接受std::string
的構造函數。這個構造函數將負責它自己的解析。
template<typename T> Base * createInstance(const std::string& s) { return new T(s); }
typedef std::map<std::string, Base*(*)(const std::string&)> map_type;
//...and similar changes as needed
如果你不想改變你的類型定義,或者這是不可接受[也許你的類型已經有了這樣一個構造函數?],擺脫模板createInstance
方法,併爲每個單獨的版本你感興趣的類型。這個函數執行解析,並調用相應的構造函數。
map["derivedA"] = createDerivedA;
map["derivedB"] = createDerivedB;
第三種選擇威力使用可變參數模板可以[或升壓式的可變參數類模板],將帶你回到原來的簡單。
template<typename T, typename... Args>
Base* create(std::string) {
//somehow pass the string to a generic function that packs the arguments into a tuple
//then somehow unpack that tuple and pass the bits to the constructor
}
map["derivedA"] = create<derivedA, int, double, std::string>;
但是,我不知道如何解決這個問題,或者即使這是可能的。
您基本上必須在鏈接問題中擴展該方法,以根據字符串中的參數數量委託給多個創建函數。如果類構造函數可以解析字符串本身,只需將其從默認構造更改爲單個字符串參數構造函數即可。 –