Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <memory>
- struct Propriety
- {
- std::string m_name;
- Propriety(std::string const& name) : m_name(name)
- {
- }
- };
- template<typename T>
- class TypedPropriety : public Propriety
- {
- public:
- TypedPropriety(std::string const& name, T const& data) : Propriety(name), m_data(data)
- {
- }
- T get()
- {
- return m_data;
- }
- private:
- T m_data;
- };
- class Proprieties_list{
- public:
- Proprieties_list(){}
- void add_propriety(Propriety prop)
- {
- std::shared_ptr<Propriety> ptr(&prop);
- propriety_list.push_back(ptr);
- }
- // cerca la proprietà con il nome dato nella lista
- template<typename T>
- T get_data(std::string const& name)
- {
- for(auto prop : propriety_list)
- {
- if(prop->m_name == name){
- TypedPropriety<T> p = dynamic_cast< TypedPropriety<T> > (*prop);
- return p.get();
- }
- }
- }
- private:
- std::vector< std::shared_ptr<Propriety> > propriety_list;
- };
- int main()
- {
- TypedPropriety<int> n_col("col", 10);
- TypedPropriety<double> mean("mean", 1.0);
- Proprieties_list plist;
- plist.add_propriety(n_col);
- plist.add_propriety(mean);
- std::string name("mean");
- std::cout << plist.get_data<double>("mean") << std::endl;
- std::cout << "Hello world!" << std::endl;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment