简体   繁体   English

减少重载函数和构造函数的数量

[英]Reducing the number of overloaded functions and constructors

Consider the following class definition: 考虑以下类定义:

class Car : public Vehicle {
    private:
        TouchScreen a;
        RadioAntenna b;
        Blah c;
    public:
        Car(TouchScreen &a1) {
            a = a1; //copy ctor
        }
        Car(RadioAntenna &b1) {
            b = b1;
        }
        Car(Blah &c1) {
            c = c1;
        }
        void update(Touchscreen &a1) {
            a.update(a1);
        }
        void update(RadioAntenna &b1) {
            b.update(b1);
        }
        void update(Blah &c1) {
            c.update(c1);
        }
}

class Fleet {
    private:
        std::map<int, Car> _fleet; //vehicle number, Car
    public:
        template <typename T>
        void update(int vehicle_num, T &t) {
            auto it = _fleet.find(vehicle_num);
            if (it == _fleet.end()) {
                _fleet[vehicle_num] = Car(t);
            }
            else {
                it->second.update(t);
            }
        }
}

Fleet contains a collection of cars. Fleet中有很多汽车。 If you want to update a specific car's member variable, 如果您要更新特定汽车的成员变量,

Fleet f;
f.update<TouchScreen>(4, a1);
f.update<Blah>(10, c1); 

In the future, more class instances can be defined inside Car. 将来,可以在Car中定义更多的类实例。 Is there a way to reduce the number of overloaded Constructor and update() functions? 有没有办法减少重载的Constructor和update()函数的数量? Using templates maybe? 也许使用模板? I feel it looks ugly, design wise, using so many overloaded functions. 我觉得使用这么多的重载函数看起来很丑陋,明智的设计。

An alternative code could be: 替代代码可以是:

void Car::update(Touchscreen* a1=nullptr, RadioAntenna* b1=nullptr, Blah* c1=nullptr ) {
        if(a1) a.update(*a1);
        if(b1) b.update(*b1);
        if(c1) c.update(*c1);                        
}

My idea is to use std::any for doing it. 我的想法是使用std :: any执行此操作。 Assumed you have only one instance for each type inside Car class. 假设Car类中每种类型只有一个实例。

Here is my solution: 这是我的解决方案:

class Car : public Vehicle {
private:
    std::unordered_map<std::string, std::any> components;
public:
    Car(std::any &obj) {
        std::string typeName(obj.type().name());
        components.insert(make_pair(typeName, obj));
    }

    template<typename T> 
    void update(T& found, T &a1)
    {
        (std::any_cast<T>(found)).update(std::any_cast<T>(a1));
    }

    void update(std::any  &a1) {
        std::string typeName(a1.type().name());
        std::any found  =  components.find(typeName);
        if (found != components.end())
        {
            if (typeName ==  std::string(typeid(TouchScreen).name())
            {
                update<TouchScreen>(found.second, a1);
            }
            else if (typeName ==  std::string(typeid(RadioAntenna ).name())
            {
                update<RadioAntenna>(found.second, a1);
            }
            else if (typeName ==  std::string(typeid(Blah  ).name())
            {
                update<Blah>(found.second, a1);
            }

        }
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM