【问题标题】:storing fixed known data in classes (c++) [closed]在类中存储固定的已知数据(c ++)[关闭]
【发布时间】:2020-09-29 22:22:32
【问题描述】:

我有这个类来存储固定的已知数据。每个对象都应该有自己的唯一 ID,这样,当用户调用 Belt belt(1) 时,会创建一个具有正确值 nameperimeternOfPoints 的对象。有没有更好的方法来定义这些其他参数而不使用 switch-case?我应该以另一种方式设计吗?这种实现有什么缺点?

class Belt {
    private:
        int _id;
        std::string _name;
        int _perimeter;
        int _nOfPoints;
    public:
        Belt(int id){
            _id = id;
            switch (id)
            {
            case 1:
                _name = "NEO";
                _perimeter = 100;
                _nOfPoints = 10;
                break;
            case 2:
                _name = "PED";
                _perimeter = 200;
                _nOfPoints = 12;
                break;
            case 3:
                _name = "ADT";
                _perimeter = 400;
                _nOfPoints = 20;
                break;
            }
        }
};

【问题讨论】:

  • 为什么不用正确的值创建static const Belts?
  • @Stephen Neweell 怎么样?通过声明静态常量结构?如果这个 Belt 类应该有数据操作的方法呢?
  • std::map 的替代方法是创建一个包含 ID 和腰带对象的结构的 const 数组。这消除了运行时初始化的需要(std::map 需要运行时初始化)。 “表”可以放在一个常量内存段中,直接访问。
  • 您可以使用 Belt 对象数组。如果你这样做了,你可以使用Belt x = belts[1]; 创建一个对象,或者如果你希望数组成为类的私有成员并将一个整数传递给构造函数,那么onlinegdb.com/H1yai4b8v

标签: c++ class dictionary oop


【解决方案1】:

有没有更好的方法来定义这些其他参数而不使用 switch-case?

您可以使用私有静态地图来保存您的原型:

class Belt {
private:
    static const std:map<int,Belt> prototypes;
    int _id;
    std::string _name;
    int _perimeter;
    int _nOfPoints;

    Belt(int id, const std::string name, int perimeter, int nOfPoints)
        : _id(id), _name(name), _perimeter(perimeter), _nOfPoints(nOfPoints) {}
public:
    Belt(int id) {   
        _id = id;
        _name = prototypes[_id]._name;  
        _perimeter= prototypes[_id]._perimeter;  
        _nOfPoints= prototypes[_id]._nOfPoints;  

        // Or simpler instead of the lines above:
        // *this = prototypes[id];
    }
};

const std:map<int,Belt> Belt::prototypes = {
      { 1 , Belt(1,"NEO",100,10) }
    , { 2 , Belt(2,"PED",200,12) }
    , { 3 , Belt(3,"ADT",400,20) }
};

您也可能有兴趣查看Prototype Design Pattern。这是您可以使用的另一种技术,并为您提供更好的灵活性。

【讨论】:

    【解决方案2】:

    如果您只想处理一组固定的值,我建议在Belt 中创建static const 实例。这具有避免任何查找开销和处理无效输入的优点(例如,有人将INT_MAX 传递给您的构造函数)。例如:

    class Belt {
        private:
            int _id;
            std::string _name;
            int _perimeter;
            int _nOfPoints;
    
            Belt(int id, name, perimeter, nOfPoints)
              : _id{id}, _name{std::move(name)}
              , _perimeter{perimeter}, _nOfPoints{nOfPoints} { }
    
        public:
            static const Belt neo(1, "NEO", 100, 10);
            static const Belt ped(2, "PED", 200, 12);
            // ...
    };
    

    用户可以通过复制现有对象之一来创建本地Belt,并且仍然可以执行他们需要的任何本地操作。

    int main() {
        auto myBelt = Belt::neo;
        myBelt.some_member_function();
    }
    
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多